C Program to Print Binary Rectangular Pattern Of 0 and 1

Here is a C program to print binary rectangle pattern where all boundary elements are 0 and all inner elements are 1. The C Program to Print Binary Rectangular Pattern of 0 and 1 is a fascinating exercise that combines the concepts of binary numbers and pattern printing in C programming.

C program binary rectangle pattern

Required Knowledge

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 binary rectangle pattern

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

Tips for Writing the Binary Rectangular Pattern Program in C

  • Use Nested Loops : Use nested loops to control the number of rows and columns printed in each iteration. The outer loop will control the rows, while the inner loop will print the binary numbers for each row.

  • Optimize the Binary Number Generation : To optimize the program, consider using efficient techniques to generate binary numbers, especially for large patterns.

  • Formatting the Output : Format the output of the program to ensure that the binary numbers are printed in the correct positions to form the rectangular pattern. Use spaces or tabs to align the numbers properly.

Conclusion

In conclusion, the C Program to Print Binary Rectangular Pattern of 0 and 1 is a valuable exercise for beginners to enhance their understanding of binary numbers, pattern recognition skills, and algorithmic thinking. Practicing this program is important for beginners to develop fundamental programming skills and build a strong foundation for their programming journey.


Related Topics
C program hollow rectangle pattern
C program rectangle star pattern
C program hollow square star pattern
C program binary triangle pattern
C program heart shape star pattern
C program same row element triangle pattern
C program prime number triangle pattern
C program palindrome triangle pattern
C program diamond star pattern
List of all C pattern programs