10001 01010 00100 01010 10001
/**
* C program to print box number pattern with cross center
* 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++)
{
if(i == j || (j == (cols+1) - i))
{
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
return 0;
}
Enter number of rows: 5
Enter number of columns: 5
10001
01010
00100
01010
10001
Basic C programming, Loop
In the given pattern, 1 is printed only when -
Below is the step by step descriptive logic to print the given number pattern.
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.