Like Fabonacci Series and Pascal’s Triangle, printing Floyd’s triangle is also a very popular problem in C programming language. In this post, the source code in C program for Floyd’s triangle has been presented along with a sample output screen. In order to make the code simple to understand, I have included multiple comments in the source code.
But, before going through the C source code, here, I would like to give a brief introduction to Floyd’s Triangle, its generation mechanism, and its important properties.
Floyd’s triangle, named after Robert Floyd, is simply a series of numbers that are sequentially spread across a series of rows. The first row of Floyd’s triangle contains a 1 by itself, and the second row contains 2 and 3. The next row holds 4, 5 and 6, and the pattern continues to infinity.
Finally, a right angled triangle of numbers separated by space comes as output. Although the programming trick in program for Floyd’s Triangle in C is not so complex, it develops integer solving skills that are essential in larger programming projects.
How to Generate Floyd’s Triangle?
In Floyd’s triangle, number of members in a row is equal to order number of row. i.e. first row has one element, second row has two elements, and so on. The first row of the triangle begins with 1 and second row with 2, element of second row are 2 and 3, again third row starts with 4 and it has 3 members 4, 5, 6, and the pattern goes like this to infinity. This basic property of this pattern has been utilized in writing the source code for Floyd’s triangle in C.
First Row 1
Second Row 2 3
Third Row 4 5 6
Fourth Row 7 8 9 10
Fifth Row 11 12 13 14 15
and so on to the infinity!
Properties of Floyd’s Triangle:
- It is a right-angled triangle of natural numbers.
- It is obtained by filling the rows with consecutive numbers, starting from 1.
- The sequence number of rows and number of elements in the row are equal, i.e. the first row has one element, the second has two, and so on.
- The numbers along the left edge of the triangle are the lazy caterer’s sequence and the numbers along the right edge are the triangular numbers.
- The sum of nth row is of Floyd’s triangle is equal to n(n2 + 1)/2.
Source Code for Floyd’s Triangle in C:
/*C Program for Floyd’s Triangle*/
#include <stdio.h>
int main() {
int n, x, y, num = 1;// n is the number of rows
printf("\n Enter the rows the Floyd's Triagle :"); // Asking for number of rows of floyd's triangle
scanf("%d",&n);
for ( x = 1 ; x <= n ; x++ )
{
for ( y = 1 ; y <= x ; y++ )
{
printf(" %d ", num); // printig the numbers
num++;
}
printf("\n"); // control goes to new line
}
return 0;
}
The aforementioned source code for printing Floyd’s triangle in C is very short and simple to understand. In the code, no user-defined function has been used. All the variables i.e. n, x, and num are of integer data type. The variable ‘n’ has been defined to store or the number of rows entered by the user. The other variables x and y are mainly for loop control, so sometimes they are known as loop control variables too.
The variable ‘num’ in this C program for Floyd’s triangle has been initialized as 1. The program needs single input, i.e. the number of rows of Floyd’s triangle to be printed. Copy and compile the source code in Code::Blocks, and you’ll get a sample output screen as shown below.
Also see,
Tower of Hanoi C Program
Fibonacci Series C Program
Pascal’s Triangle C Program
The above source code is totally error-free and has been coded perfectly to eradicate bugs. If you have any other alternative C source codes related to Floyd’s triangle, share them with us. Your queries, suggestions, and feedback regarding this C program for Floyd’s triangle, can be discussed in and brought directly to us from the comments box below.