How to write a C program to Print Number Pattern 1 with example. Or, C Program to Print Rectangle Number Pattern.
C program to Print Number Pattern 1 using For Loop
This program allows the user to enter the maximum number of rows and columns he/she want to print as a rectangle. Next, compiler will print the numbers from 1 to user specified rows.
/* C program to Print Number Pattern 1 */ #include<stdio.h> int main() { int i, j, rows, columns; printf(" \nPlease Enter the Number of Rows : "); scanf("%d", &rows); printf(" \nPlease Enter the Number of Columns : "); scanf("%d", &columns); for(i = 1; i <= rows; i++) { for(j = 1; j <= columns; j++) { printf("%d", i); } printf("\n"); } return 0; }

Let us see the Nested for loop
for(i = 1; i <= rows; i++) { for(j = 1; j <= columns; j++) { printf("%d", i); } printf("\n"); }
Outer Loop – First Iteration
From the above C Programming screenshot you can observe that, The value of i is 7 and the condition (i <= 7) is True. So, it will enter into second for loop
Inner Loop – First Iteration: The j value is 1 and the condition (1 <= 9) is True. So, it will start executing the statements inside the loop.
printf(" %d", i);
Following statement will be incremented by 1 using the Increment Operator
j++
This will happen until the condition inside the inner for loop fails. Next, iteration will start from beginning until both the Inner Loop and Outer loop conditions fails.
Program to Print Number Pattern 1 using while Loop
In this program we just replaced the For Loop with the While Loop. I suggest you to refer While Loop article to understand the logic.
/* C program to Print Number Pattern 1 */ #include<stdio.h> int main() { int i, j, rows, columns; i = 1; printf(" \nPlease Enter the Number of Rows : "); scanf("%d", &rows); printf(" \nPlease Enter the Number of Columns : "); scanf("%d", &columns); while(i <= rows) { j = 1; while(j <= columns) { printf("%d", i); j++; } i++; printf("\n"); } return 0; }
