C program to Print Hollow Box Number Pattern

How to write a C program to Print Hollow Box Number Pattern with example?. For this Hollow Box Number Pattern, we are going to use For Loop and While Loop.

C program to Print Hollow Box Number Pattern Example 1

This program allows the user to enter rows and column values. Here, we are going to print the hollow Box number pattern of 1’s. I mean, it will print First row, last row, first column, and last column as 1’s, and the remaining elements as empty.

First, we used the Nested for loop to iterate each row and column element. Next, we used the If statement inside the Nested For Loop to check whether it is the First row, First Column, Last row, or last column.

/* C program to Print Hollow Box Number Pattern */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    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("1");
			}
			else
			{
				printf(" ");
			}       	
        }
        printf("\n");
    }
    return 0;
}
C program to Print Hollow Box Number Pattern 1

Program to Print Hollow Box Number Pattern Example 2

This hollow box number C program is the same as the first example, but this time we are using the While Loop (Just replacing C Programming For Loop with While Loop).

/* C program to Print Hollow Box Number Pattern */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
    i = 1;
     
    printf(" \nPlease Enter the Number of Rows : ");
    scanf("%d", &rows);
    
    printf(" \nPlease Enter the Number of Columns : ");
    scanf("%d", &columns);
     
    while(i <= rows)
    {
    	j = 1;
    	while(j <= columns)
		{
			if(i == 1 || i == rows || j == 1 || j == columns)
			{
				printf("1");
			}
			else
			{
				printf(" ");
			}  
			j++;     	
        }
        i++;
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 10
 
Please Enter the Number of Columns : 22
1111111111111111111111
1                    1
1                    1
1                    1
1                    1
1                    1
1                    1
1                    1
1                    1
1111111111111111111111

Program to Print Hollow Box Number Pattern Example 3

This program is the same as the above example, but we changed the printing number. I mean, this program will print the Hollow box number pattern of 0’s. So, the compiler will print 0’s in the First row, last row, first column, and last column.

/* C program to Print Hollow Box Number Pattern */

#include<stdio.h>
 
int main()
{
    int i, j, rows, columns;
     
    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("0");
			}
			else
			{
				printf(" ");
			}       	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 12
 
Please Enter the Number of Columns : 24
000000000000000000000000
0                      0
0                      0
0                      0
0                      0
0                      0
0                      0
0                      0
0                      0
0                      0
0                      0
000000000000000000000000