Print Hollow Rectangle Star Pattern

PRINTING PATTERN:

******

*        *

*        *

******

PREREQUISITE:

Basic knowledge of C language and loops.

ALGORITHM:

  1. Take the number of rows and columns as input from the user ( length and breadth of the rectangle) and store it in two different variables. (‘r’ and ‘c’ in this case)
  2. Run a loop ‘r’ number of times to iterate through all the rows. From i=0 to i<r. The loop should be structured as for(i=0 ; i<r ; i++)
  3.  Run a nested loop ‘c’ times to iterate though each column of a row. From j=0 to j<c. The loop should be structured as for(j=0 ; j<c ; j++).
  4. Inside the nested loop print star if i=0 or i=r-1 or j=0 or j=c-1.
  5. Else print a white-space.
  6. Inside the main loop print a newline to move to the next line.

CODE IN C:

#include<stdio.h>
int main()
{
int i,j,r,c;       //declaring integers i,j for loops and r,c for number of rows and columns
printf("Enter the number of rows\n");      //asking user for number of rows
scanf("%d",&r);     //saving number of rows in a variable r
printf("Enter the number of columns\n");     //asking user for number of columns
scanf("%d",&c);     //saving number of columns in a variable c
for(i=0;i<r;i++)    //outer loop for number of rows
   {
      for(j=0;j<c;j++)     //inner loop for number of columns
         {
            if(i==0 || i==r-1 || j==0 || j==c-1)    //conditions for printing stars in a given column
               {
                  printf("*");    //printing stars
               } 
            else      //else condition to print whitespace
               {
                  printf(" ");    //printing whitespace
               }
         }
      printf("\n");     //printing newline after a row
   }
}

TAKING INPUT:
DISPLAYING OUTPUT: