Example:
Input rows: 5
Input columns: 5
Output:
10001 01010 00100 01010 10001
Required knowledge
Basic C programming, LoopLogic to print box number pattern with cross center
Before moving on to this pattern I recommend you to go through the previous number pattern to get yourself acquainted with the number patterns logic.Now, once you are acquainted with basic of number pattern. If you look to this pattern carefully you will notice a special pattern in which 1's gets printed here. Actually, 1's only gets printed in one of two conditions:
- If the current column equals to current row.
- If the current column equals (total columns+1) - current row.
Program to print box number pattern with cross center
/** * C program to print box number pattern with cross center */ #include <stdio.h> int main() { int rows, cols, i, j; /* * Reads number of rows, columns to be printed */ 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; }
Output
Enter number of rows: 5
Enter number of columns: 5
Enter number of columns: 5
10001 01010 00100 01010 10001
Screenshot
Fun with patterns: To have more fun with these patterns just change the character which you want to be printed i.e. the inner two printf(); statements and get the following patterns.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1111111 1 11111 1 11 111 11 111 1 111 1111 1111 111 1 111 11 111 11 1 11111 1 1111111
X X X X X X X X X X X X X X X X X
X-------X -X-----X- --X---X-- ---X-X--- ----X---- ---X-X--- --X---X-- -X-----X- X-------X
Happy coding ;)