C program to print 0 or 1 square number pattern
11111
11111
11111
11111
11111
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
4892Program:
/** * C program to print square number pattern * atnyla.com */ #include 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: 5 11111 11111 11111 11111 11111
Explanation:
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.
Note: To print rectangle number pattern, make the rows and columns different.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.