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

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 C Nested for loop to iterate each row and column element. Next, we used the C If statement inside the Nested For Loop to check whether it is the First row, First Column, Last row, or last column.

#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 program is the same as the first example, but this time we are using the C Programming While Loop (Just replacing C Programming For Loop with While Loop). Check out the C tutorial.

#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 C 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. For more number pattern, please refer to the C Number Pattern Programs article.

#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

Also Read