C Program to Print Floyd’s Triangle

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 an example.

C Program to Print Floyd’s Triangle

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

#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 first two statements allow the user to enter the range or maximum Number of rows to print.

Now let us see the Nested for loop logic in detail.

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

Outer Loop – First Iteration

From the above C programming screenshot, you can observe that the value of i is 1, Rows is 5, and the condition (i <= 5) is True. So, it will enter into the 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 Number++ will increment the Value of the Number by 1 using the Increment Operator.

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 the 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, Number++, will increment the Value of the Number by 1.

Next, the j value will also be 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 Print Floyd’s Triangle without Natural Numbers

This program allows the user to enter the number of rows to print as Floyd’s triangle. In this C example program, we are going to print Floyd’s triangle using the * symbol. This program can also be said to print the Right-angled triangles with * symbols.

#include <stdio.h>

int main() 
{
  int Rows, i, j;
	
  printf(" Please Enter the Number of Rows:  ");
  scanf("%d", &Rows);
	
  printf(" \n \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
 
 
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * *