Introduction
Floyd's Triangle is a right-angled triangle consisting of an array of natural numbers named after its founder Robert W. Floyd.
It is formed by starting with the number 1 and printing a row of one number, followed by incrementing the number of rows until the desired number of rows gets printed. Each row starts with the following consecutive number after the previous row.
Let us learn more about Floyd's Triangle in C.

Also Read, Binary to Hex Converter.C Static Function
What is Floyd's triangle in C?
A right-angled triangle with successive natural integers is known as Floyd's triangle. The number in Floyd's triangle starts with 1 in the top left corner and continues to fill the defined rows with numbers. In this blog, we will further work on the implementation of Floyd's triangle in c.
Example
If we have to print 6 rows of Floyd's triangle it will be in the following way:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Explanation
Print the normal counting of numbers and in each row, print a count of a number equal to the row number for example, in row 1, print 1 element, then in row 2, print 2 elements, and so on.
Algorithm
- Create the variables for holding row number and column number as i and j. Then further take a number to display the rows as 'row' and set the variable count to 1 which will print the elements.
- Now Use the concept of nested loops.
- The iteration of the outer for loop begins with I = 1 and continues up to total rows.
- Iteration of the inner for loop begins at j = 1 and ends at (j=i).
- Print the count values.
- Increase count by one or make count = count + 1.
- After each iteration of the inner for loop, jump to a new line.
- Stop
Program to print the Floyd's triangle using for loop
#include <stdio.h>
void main()
{
int n,i,j,k = 1;
printf( "Number of rows: ");
scanf( "%d", &n);
// Outer for loop to define the rows.
for (i = 1; i <= n; i++)
{
// Inner for loop for checking the value of j.
for (j = 1; j <= i; j++)
{
printf(" %2d ", k++);
}
printf( "\n");
}
return ;
}
Output
Number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
In the above example, we have to print 5 rows of Floyds’s Triangle. For that, we use a nested for loop to print the triangle.
The first or the outer for loop will count using the variable i as 1 and will check the condition where i <= n. The second or the inner for loop works through j= 1 till j = i.
Program to print the Floyd's triangle using while loop
#include<stdio.h>
void main()
{
int n, i = 1, j = 1, k;
printf("Number of rows: ");
scanf("%d", &n);
printf("\n");
while(j <= n)
{
k = 1;
while(k <= j)
{
printf("%d ", i);
i++;
k++;
}
j++;
printf("\n");
}
return 0;
}
Output
Number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
In this program we printed 5 rows of Floyd’s Triangle using nested while loops.
Program to print the Floyd's triangle using the Recursion function
#include <stdio.h>
// Declaring of global variables
int rows = 1;
int k = 1;
// Defining the function
void Floyd_triangle (int n)
{
int i;
if (n <= 0)
return;
for ( i = 1; i <= rows; i++)
printf( " %d", k++);
printf ("\n");
rows++;
Floyd_triangle (n - 1);
}
int main()
{
int n;
printf( " Number of rows: ");
scanf (" %d", &n);
// Calling Floyd_triangle function
Floyd_triangle( n);
return 0;
}
Output
Number of rows: 6
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
In this program, we printed 6 rows of Floyd’s Triangle using the recursion function and for loop to define the rows.
Program to reverse the Floyd's triangle using for loop
#include <stdio.h>
int main()
{
int n, i, j, k;
printf ("Number of rows: ");
scanf (" %d", & n);
//Formula to get the reverse Floyd's triangle
k = n * ( n + 1) / 2;
//Outer loop
for (i = n; i >= 0; i--)
{
//Checking the i condition
for (j = 1; j <= i; j++)
{
printf (" %d", k--);
}
printf ("\n");
}
return 0;
}
Output
Number of rows: 7
28 27 26 25 24 23 22
21 20 19 18 17 16
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
In this program, we formed Floyd’s Triangle in a reversed order using nested for loops.
Program to print the star Floyd's triangle using for loop
#include <stdio.h>
void main()
{
int n, i, j, k = 1;
printf( "Number of rows: ");
scanf( "%d", &n);
// outer for loop define the rows and check rows condition
for (i = 1; i <= n; i++)
{
// inner loop check j should be less than equal to 1 and print the data.
for (j = 1; j <= i; j++)
{
printf(" * ");
}
printf( "\n");
}
return ;
}
Output
Number of rows: 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
In this program, we made a Floyd’s Triangle using stars. We used nested for loops where the first or outer for loop will count using the variable i as 1 and will check the condition where i <= n. The second or the inner for loop works through j= 1 till j = i.
Program to print the Alphabets Floyd's triangle in C using for loop
#include <stdio.h>
void main()
{
int n, i, j, k = 'A';
printf( "Number of rows: ");
scanf( "%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf(" %c", k);
k++;
}
printf( "\n");
}
return 0;
}
Output
Number of rows: 5
A
B C
D E F
G H I J
K L M N O
In this program, we made a Floyd’s Triangle using alphabets. We used nested for loops where the first or outer for loop will count using the variable i as 1 and will check the condition where i <= n. The second or the inner for loop works through j= 1 till j = I.
Frequently Asked Questions
What is a nested loop?
A nested loop is an inner loop inside the body of an outer loop. The inner loop is triggered by the first pass of the outer loop, which executes to completion. The inner loop is then triggered by the outer loop's second pass. This continues until the outer loop is completed. This process would be disrupted by a break in either the inner or outer loop. We can use nested loop to print the Floyd's triangle in C.
Why is this triangle pattern given a name as Floyd's triangle?
It was named after a computer scientist Robert w. Floyd.
What is the most observing property of Floyd's triangle?
By the index of each row, each number in the triangle is less than the number below it.
What is the use of printf and scanf in C?
We have used printf to print the elements of Floyd's triangle in C printf is used to print a statement in C and scanf is used to taking the user input in C.scanf in the implementation is used to taking the user input for the number of rows. Here the number of rows will signify the number of rows in Floyd's triangle in C.
What is the real life application of Floyd's triangle?
To a mathematician, it isn't really relevant. Its primary purpose is to serve as a beginner's computer programming exercise. Aside from that, it's a good example of what's known as "triangular numbers."
Conclusion
In this article, we have extensively discussed the solution to print floyd's triangle in c along with its time and space complexity.
Until then, All the best for your future endeavors, and Keep Coding. "We hope that this blog has helped you enhance your knowledge regarding this problem, and if you would like to learn more, check out our articles on CodeStudio. Do upvote our blog to help other ninjas grow. Happy Coding!