# C Program for Floyd’s Triangle

0
3679

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 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 important properties.

Floyd’s triangle, named after Robert Floyd, is simply a series of numbers which 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 by consecutive numbers, starting from 1.
• Sequence number of row and number of element in the row are equal, i.e. first row has one element, 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:

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 variable 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. 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 feedbacks regarding this C program for Floyd’s triangle, can be discussed in, and brought directly to us from the comments box below.