11111 11111 11011 11111 11111
/**
* C program to print box number pattern of 1's with 0 as center
*/
#include <stdio.h>
int main()
{
int rows, cols, i, j;
int centerRow, centerCol;
/* Input rows and columns from user */
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
/* Find center row and column */
centerRow = (rows + 1) / 2;
centerCol = (cols + 1) / 2;
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
if(centerCol == j && centerRow == i)
{
printf("0");
}
else if(cols%2 == 0 && centerCol+1 == j)
{
if(centerRow == i || (rows%2 == 0 && centerRow+1 == i))
printf("0");
else
printf("1");
}
else if(rows%2 == 0 && centerRow+1 == i)
{
if(centerCol == j || (cols%2 == 0 && centerCol+1 == j))
printf("0");
else
printf("1");
}
else
{
printf("1");
}
}
printf("\n");
}
return 0;
}
Enter number of rows: 5
Enter number of columns: 5
11111
11111
11011
11111
11111
Basic C programming, Loop
11111 11111 11011 11111 11111
In the above given pattern for every row and column 1 is printed except for the center row and column. Hence, to print this pattern we only need to focus on condition of center row and column.
Below is the step by step descriptive logic to print given pattern.
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.