- Write a C program to print binary rectangle pattern where all boundary elements are 0 and all inner elements are 1.
Required Knowledge
- For Loop
- If Else Statements
- printf and scanf function in C
Algorithm to print rectangular pattern of binary digit 0 and 1
This program is similar to rectangle star pattern. At every position of rectangle we will check for boundary condition (first and last row and column of rectangle). If true, then we print 0 othewise 1.
Here is the matrix representation of the above mentioned binary pattern. The row numbers are represented by i whereas column numbers are represented by j.
C program to print binary rectangle pattern
#include<stdio.h> int main(){ int rows, cols , i, j; printf("Enter rows and columns of rectangle\n"); scanf("%d %d", &rows, &cols); /* Row iterator for loop */ for(i = 0; i < rows; i++){ /* Column iterator for loop */ for(j = 0; j < cols; j++){ /* Check if currely position is a boundary position */ if(i==0 || i==rows-1 || j==0 || j==cols-1) printf("0"); else printf("1"); } printf("\n"); } return 0; }
Output
Enter rows and columns of rectangle 4 7 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0