C Program to Print Hollow Rectangle Star Pattern

How to write a C Program to Print Hollow Rectangle Star Pattern with example?. And also show you how to print a hollow rectangle pattern with different symbols.

C Program to Print Hollow Rectangle Star Pattern

This C program allows the user to enter the number of rows and columns to design a hollow rectangle. This value will decide the number of rows and columns of a hollow rectangle. Here, we are going to print the stars until it reaches the user-specified rows and columns.

/* C Program to Print Hollow Rectangle Star Pattern */
#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    printf("Please Enter the Number of rows:\n");
    scanf("%d", &rows);
    
    printf("Please Enter the Number of Columns:\n");
    scanf("%d", &columns);
	 
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < columns; j++)
	{
	  if(i == 0 || i == rows-1 || j == 0 || j == columns-1)
	  {
		printf("*");
	  }
	  else
	  {
		printf(" ");
	  }          
        }
        printf("\n");
    }
    return 0;
}
C Program to Print Hollow Rectangle Star Pattern 1

Let us see the Nested for loop in the C Program to Print Hollow Rectangle Star Pattern example

for(i = 0; i < rows; i++)
{
   for(j = 0; j < columns; j++)
   {
     if(i == 0 || i == rows-1 || j == 0 || j == columns-1)
     {
	printf("*");
     }
     else
     {
	printf(" ");
     }          
   }
   printf("\n");
}

Outer Loop – First Iteration

From the above C Programming screenshot, you can observe that the value of i is 0 and Rows is seven so, the condition (i < 5) is True. So, it will enter into second for loop

Inner Loop – First Iteration

The j value is 0 and the condition (j < 15) is True. So, it will start executing the statement inside the loop. So, it will enter into the If Statement. Here, we are checking whether i = 0, j = 0, i = 6 (rows – 1) , or j = 14 (columns – 1). If any of this condition is true, then the following statement will print *. It means, Only the first column and last column in each row will print as output, and the remaining columns will print as an empty space.

printf("*");

It will happen until it reaches 7, and after that, both the Inner Loop and Outer loop will terminate.

C Program to Print Hollow Rectangle Pattern with Dynamic Character

This C program allows the user to enter the Symbol that he/she wants to print as a hallow rectangle pattern.

/* C Program to Print Hollow Rectangle Star Pattern */
#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
    char Ch;
	
    printf("Please Enter any Symbol:  ");
    scanf("%c", &Ch);    
     
    printf("\nPlease Enter the Number of rows:  ");
    scanf("%d", &rows);
    
	printf("\nPlease Enter the Number of Columns:  ");
    scanf("%d", &columns);
	 
    for(i = 1; i <= rows; i++)
    {
        for(j = 1; j <= columns; j++)
	{
	  if(i == 1 || i == rows || j == 1 || j == columns)
	  {
		printf("%c", Ch);
	  }
	  else
	  {
		printf(" ");
	  }          
        }
        printf("\n");
    }
    return 0;
}
Please Enter any Symbol:  #

Please Enter the Number of rows:  10

Please Enter the Number of Columns:  20
####################
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
#                  #
####################