Output a box with given width and height - C Statement

C examples for Statement:while

Description

Output a box with given width and height

Demo Code

#include <stdio.h>

int main(void)
{
  const unsigned int MIN_SIZE = 3;          // Minimum width and height values
  unsigned int width = 0;
  unsigned int height = 0;

  printf("Enter values for the width and height (minimum of %u):", MIN_SIZE);
  scanf("%u%u", &width, &height);

  if(width < MIN_SIZE){
    width = MIN_SIZE;//  w w w  .j a v  a  2s.  c  o  m
  }
  if(height < MIN_SIZE){
    height = MIN_SIZE;
  }

  for(unsigned int i = 0 ; i < width ; ++i)
    printf("*");

  for(unsigned int j = 0 ; j < height - 2 ; ++j)
  {
    printf("\n*");                               // First asterisk

    // Next draw the spaces
    for(unsigned int i = 0 ; i < width - 2 ; ++i)
      printf(" ");

    printf("*");                                 // Last asterisk

  }
  // Output the bottom of the box
  printf("\n");                                  // Start on newline
  for(unsigned int i = 0 ; i < width ; ++i)
    printf("*");

  printf("\n");                                  // Newline at end of last line
  return 0;
}

Result


Related Tutorials