C Program to Print Floyd’s Triangle

The Floyd’s Triangle is a right angled triangle with an array of natural numbers. This article shows how to write a C Program to Print Floyd’s Triangle with example.

C Program to Print Floyd’s Triangle

This C program allows the user to enter the maximum number of rows he/she want to print as Floyd’s triangle. We are going to print Floyd’s triangle of integers until it reaches the user specified rows.

/* C Program to Print Floyd’s Triangle */
#include <stdio.h>

int main() 
{
  int Rows, i,  j, Number = 1;

  printf(" Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf(" \n Printing FLOYD'S Triangle \n \n");
  for ( i = 1 ; i <= Rows; i++ ) 
    {
	for ( j = 1 ; j <= i; j++ ) 
         {
	   printf("%d ", Number);
	   Number++;
	 }
	printf("\n");
     }
  return 0;
}
C Program to Print Floyd’s Triangle 1

The following statements allow the user to enter the range or maximum Number of rows he/she want to print.

 printf(" Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);

Now let us see the Nested for loop

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

Let us see the execution process of C Program to Print Floyd’s Triangle in an iteration wise

Outer Loop – First Iteration

From the above C programming screenshot, you can observe that the value of i is 1 and Rows is 5, and the condition (i <= 5) is True. So, it will enter into second for loop

Inner 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 Number as Output, and we all know that Number = 1 so, 1 is printed

printf("%d ", Number);

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

Number++;

Inner Loop – Second Iteration

The j value will be 2, and the condition (2 <= 1) is False so that it will exit from the second loop. The following announcement is to terminate the current line

printf("\n");

Outer Loop – Second Iteration
The value of i will be 2, and the condition (2 <=5) is True. So, it will enter into second for loop

Inner Loop – First Iteration

The value of j is 1, and the condition (1 <= 2) is True. So, it will start executing the statements inside the loop. The following statement will print Number as Output, and we all know that Number = 2 so, 2 is printed

printf("%d ", Number);

The following statement will increment the Value of Number by 1

Number++;

Next, the j value will also incremented by 1.

Inner Loop – Second Iteration

The value of j is 2, and the condition (2 <= 2) is True. So, it will start executing the statements inside the loop. It means 3 will print.

Inner Loop – Third Iteration
The j value will be 3 and the condition (3 <= 2) is False so it will exit from the second loop.

It happens until it reaches 5, and after that, both the Inner Loop and Outer loop will terminate.

C Program to Display Floyd’s Triangle without Natural numbers

This program allows the user to enter the number of rows he/she want to print as Floyd’s triangle. In this example we are going to print the Floyd’s triangle using the * symbol. This program can also be said as print Right angled triangle with * symbol’s.

/* C Program to Print Floyd’s Triangle */
#include <stdio.h>

int main() 
{
  int Rows, i, j;
	
  printf(" Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf(" \n Printing FLOYD'S Triangle \n \n");
  for ( i = 1 ; i <= Rows; i++ ) 
    {
      for ( j = 1 ; j <= i; j++ ) 
	{
          printf("* ");
	}
      printf("\n");
    }
  return 0;
}
 Please Enter the Number of Rows:  10
 
 Printing FLOYD'S Triangle 
 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * *