11111 10001 10001 10001 11111
/**
* C program to print box number pattern of 1's and 0's
* www.atnyla.com
*/
#include <stdio.h>
int main()
{
int rows, cols, i, j;
/* Input rows and columns from user */
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
/*
* Print 1 if its first or last row
* Print 1 if its first or last column
*/
if(i==1 || i==rows || j==1 || j==cols)
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
return 0;
}
Enter number of rows: 5
Enter number of columns: 5
11111
10001
10001
10001
11111
Basic C programming, Loop
If you look carefully to this pattern you will find that 1 is printed for.
Below is the step by step descriptive logic to print the given pattern.
Note: You can also reverse the given number pattern with 0's as border and 1's at the center. For that you just need to swap the inner two printf("1"); with printf("0"); statement. You can further play with the given pattern to print
11111 1 1 1 1 1 1 11111
To print above pattern you just need to change single line in the above program. Replace the inner printf("0"); with printf(" ");
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.