Write a C program to Print Number Pattern 4 with example. For this, we are going to use For Loop and While Loop.
C program to Print Number Pattern 4 using For Loop
This program allows the user to enter the maximum number of rows he/she want to print as a right triangle. Next, compiler will print the required numbers pattern.
/* C program to Print Number Pattern 4 */
#include<stdio.h>
int main()
{
int i, j, rows;
printf(" \nPlease Enter the Number of Rows : ");
scanf("%d", &rows);
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}

Let us see the Nested for loop
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d", j);
}
printf("\n");
}
Outer Loop – First Iteration
From the above screenshot you can observe that, The value of i is 5 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 <= 1) is True. So, it will start executing the statements inside the loop.
printf("%d", j);
Next, we used the Increment Operator j++ to increment the J value by 1. This will happen until the C Programming 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 4 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 4 */
#include<stdio.h>
int main()
{
int i, j, rows;
i = 1;
printf(" \nPlease Enter the Number of Rows : ");
scanf("%d", &rows);
while(i <= rows)
{
j = 1;
while( j <= i)
{
printf("%d", j);
j++;
}
i++;
printf("\n");
}
return 0;
}
Please Enter the Number of Rows : 9
1
12
123
1234
12345
123456
1234567
12345678
123456789