C Program to Print Right Triangle Number Pattern

How to write a C Program to Print Right Triangle Number Pattern with example.? Or, Program to Print Right-angled Triangle Number Pattern.

C Program to Print Right Triangle Number Pattern Example 1

This program allows the user to enter the maximum number of rows he/she want to print as the Right Triangle (or right angled triangle). Next, the compiler will print the Right Angled Triangle of consecutive numbers until it reaches the user specified rows.

/* C Program to Print Right Triangle Number Pattern */

#include <stdio.h>
 
int main() 
{
  	int rows, i, j;
	
  	printf(" \n Please Enter the Number of Rows:  ");
  	scanf("%d", &rows);
	
  	for ( i = 1 ; i <= rows; i++ ) 
  	{
      	for ( j = 1 ; j <= i; j++ ) 
      	{
          	printf(" %d", i);
      	}
      	printf("\n");
  	}
  	return 0;
}
C Program to Print Right Triangle Number Pattern 1

Let us see the Nested for loop

for ( i = 1 ; i <= rows; i++ ) 
{
      	for ( j = 1 ; j <= i; j++ ) 
      	{
          	printf(" %d", i);
     	}
     	printf("\n");
}

First For Loop – First Iteration: From the above C Programming screenshot, you can see the value of i is 1 and Rows is 8 so, the condition (i <= 8) is True. So, it will enter into second for loop

Second For Loop – First Iteration

The j value is 1 and the condition (j <= 1) is True. So, it will start executing the statements inside the loop. The following statement will print 1 as Output

printf(" %d", i);

The following statement will increment the Value of Number by 1 using the Increment Operator

j++

It happens until the condition inside the inner for loop fails. Next, iteration will start from the beginning until both the Inner Loop and Outer loop conditions fail.

Program to Print Right Triangle Number Pattern Example 2

In this right triangle of numbers C program, we just replaced the For Loop with the While Loop.

/* C Program to Print Right Triangle Number Pattern */

#include <stdio.h>
 
int main() 
{
  	int rows, i, j;
  	i = 1;
	
  	printf("Please Enter the Number of Rows:  ");
  	scanf("%d", &rows);
	
  	while ( i <= rows) 
  	{
  		j = 1;
      	while ( j <= i ) 
      	{
        	printf("%d", i);
          	j++;
      	}
      	i++;
      	printf("\n");
  	}
  	return 0;
}
Please Enter the Number of Rows:  9
1
22
333
4444
55555
666666
7777777
88888888
999999999