C - Draw a box using for loop

Introduction

To draw a box using asterisks, consider the following code.

Demo

#include <stdio.h>

int main(void)
{
      printf("\n**************");         // Draw the top of the box

      for(int count = 1 ; count <= 8 ; ++count)
        printf("\n*            *");       // Draw the sides of the box

      printf("\n**************\n");       // Draw the bottom of the box
      return 0;/*from   ww w.j  ava2  s. com*/
}

Result

how It Works

The first printf() statement outputs the top of the box:

printf("\n**************");           // Draw the top of the box

The next statement is the for loop:

for(int count = 1 ; count <= 8 ; ++count)
      printf("\n*            *");         // Draw the sides of the box

This repeats the printf() statement eight times to output the sides of the box.

Related Topic