Write a C program to print the given 0, 1 square number pattern using loop. C program to print binary number pattern of n rows and m columns using loop. How to print the square number patterns using for loop in C programming. Logic to print the square filled with 1 using for loop in C program.
Example
Input
Input rows: 5 Input columns: 5
Output
11111 11111 11111 11111 11111
Required knowledge
Logic to print square number pattern
Logic to print this square number pattern of 1 is simple and similar to square start pattern.
We only need to replace the stars(*) with 1 or 0 whatever you want to print. Basic logic to print square number pattern of n rows and m columns.
Below is the step by step descriptive logic to print square number pattern.
- Input number of rows and columns to print from user. Store it in some variable say rows and cols.
- To print square number pattern, we need two loops. An outer loop to iterate through rows and second an inner loop to iterate through columns.
- Run an outer loop from 1 to total rows. The loop structure should look like for(i=1; i<=rows; i++).
- Inside the outer loop run an inner loop from 1 to total columns. The loop structure should look like for(j=1; j<=cols; j++).
- Inside the inner loop, print whatever you want to get printed as output, in our case print 1.
- After inner loop, advance the cursor position to next line i.e. print a dummy blank line.
Program to print square number pattern
/**
* C program to print square number pattern
*/
#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);
/* Iterate through rows */
for(i=1; i<=rows; i++)
{
/* Iterate through columns */
for(j=1; j<=cols; j++)
{
printf("1");
}
printf("\n");
}
return 0;
}
Output
Enter number of rows: 5 Enter number of columns: 511111 11111 11111 11111 11111
Note: To print rectangle number pattern, make the rows and columns different.
Happy coding 😉