C program to Print Box Number Pattern of 1 and 0

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

C program to Print Box Number Pattern of 1 and 0 Example 1

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

First, we used the C Programming Nested for loop to iterate every 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.

/* C program to Print Box Number Pattern of 1 and 0 */

#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("0");
			}       	
        }
        printf("\n");
    }
    return 0;
}
C program to Print Box Number Pattern of 1 and 0 1

Program to Print Box Number Pattern of 1 and 0 Example 2

This box pattern of numbers 1 and 0 program is the same as the first example. However, this time we are using a C programming While Loop (Just replacing For Loop with While Loop). Please check out the C tutorial page.

/* C program to Print Box Number Pattern of 1 and 0 */

#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("0");
			}  
			j++;     	
        }
        i++;
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 8
 
Please Enter the Number of Columns : 12
111111111111
100000000001
100000000001
100000000001
100000000001
100000000001
100000000001
111111111111

Program to Print Box Number Pattern of 1 and 0 Example 3

This C program is same as above example but we changed the printing number. I mean, this program will print 0’s in the First row, last row, first column, and last column. For more numeric example, please refer to the Number Pattern Programs in C article.

/* C program to Print Box Number Pattern of 1 and 0 */

#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("1");
			}       	
        }
        printf("\n");
    }
    return 0;
}
Please Enter the Number of Rows : 8
 
Please Enter the Number of Columns : 20
00000000000000000000
01111111111111111110
01111111111111111110
01111111111111111110
01111111111111111110
01111111111111111110
01111111111111111110
00000000000000000000

Also Read