C Program to Print Floyd’s Triangle

Floyds Triangle in C is a right-angled triangular array of natural numbers. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner: 1. Number of rows of Floyd’s triangle to print is entered by the user. For loop is used to print the output of the program.

Example:
A Floyd’s triangle is a triangle in which each number is the sum of the two numbers above it. For example, the first row of the Floyd’s triangle is 1, the second row is 2 + 1 = 3, and so on. The following is a diagram of the Floyd’s triangle:

1  
2 3  
4 5 6  
7 8 9 10
Problem Description

Write a C program to display floyd’s triangle.

Problem Solution

1. Take the number of rows as input.
2. Store the number of rows in a variable.
3. Create a for loop to loop through the number of rows.
4. Print the elements for every row.

Let’s discuss different ways to display floyd’s triangle in C language.

advertisement
advertisement
Method 1: Floyds Triangle in C using For Loop

In this approach, we’ll use a for loop to loop through the number of rows and print the elements for every row by incrementing the number of rows by 1.

Example:
Given Rows = 5
Then the floyds triangle can be seen as

   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15
Program/Source Code

Here is source code of the C Program to display floyd’s triangle using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Display Floyd’s triangle using For Loop
 */
 
#include <stdio.h>
 
void floyds_triangle(int rows)
{
    int i, j, k = 1;
    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%4.d ", k ++);
        }
        printf("\n");
    }
}
 
int main(void)
{
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
 
    floyds_triangle(rows);
 
    return 0;
}
Program Explanation

1. The program asks the user to enter the number of rows and save the value in a row variable and returns a Floyd triangle with the same height.
2. The program will print the elements for every row by incrementing the number of rows by 1.
3. Print the elements for every row.

Time complexity: O(n)
The time complexity of displaying the Floyd triangle is O(n).

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, the entered value is “5” to print the floyds triangle.

advertisement
Enter the number of rows: 5
   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15

Testcase 2: In this case, the entered value is “12” to print the floyds triangle.

Enter the number of rows: 12
   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15 
  16   17   18   19   20   21 
  22   23   24   25   26   27   28 
  29   30   31   32   33   34   35   36 
  37   38   39   40   41   42   43   44   45 
  46   47   48   49   50   51   52   53   54   55 
  56   57   58   59   60   61   62   63   64   65   66 
  67   68   69   70   71   72   73   74   75   76   77   78

Method 2: Floyds Triangle in C using While Loop

In this approach, we’ll use a while loop to loop through the number of rows and print the elements for every row by incrementing the number of rows by 1.

advertisement

Example:
Given Rows = 7
Then the floyds triangle can be seen as

   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15 
  16   17   18   19   20   21 
  22   23   24   25   26   27   28
Program/Source Code

Here is source code of the C Program to display floyd’s triangle using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Display Floyd’s triangle using while loop
 */
#include <stdio.h>
 
int main()
{
    int i = 1, j = 0, rows = 7;
    int num = 1;
    while (i <= rows) 
    {
        while (j <= i - 1) 
        {
            printf("%d ", num);
            j++;
            num++;
        }
        j = 0;
        i++;
        printf("\n");
    }
    return 0;
}
Program Explanation

1. While loop is used to print the floyds triangle.
2. While loops check the condition and repeat the loop until the condition is false.
3. Print the elements for every row.

Time complexity: O(n)
The time complexity of displaying the Floyd triangle is O(n).

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

In this case, the entered value is “7” to print the floyds triangle.

Enter the number of rows: 7
   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15 
  16   17   18   19   20   21 
  22   23   24   25   26   27   28

Method 3: Floyds Triangle in C using Recursion

In this approach, it takes the number of rows as input and stores the number of rows in a variable. Create a recursive function to print the floyd’s triangle.

Methods used:
void floydTriangleRecursive(int, int) – This function will print the floyd’s triangle for the given number of rows. The parameters are the number of rows and the curent row index.

Example:
Given Rows = 10
Then the floyds triangle can be viewed as

Enter the number of rows: 10
   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15
  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31  32  33  34  35  36
  37  38  39  40  41  42  43  44  45
  46  47  48  49  50  51  52  53  54  55
Program/Source Code

Here is source code of the C Program to display floyd’s triangle using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Display Floyd’s triangle using recursion
 */
#include <stdio.h>
 
void floydTriangleRecursive(int a, int b)
{
    if (a == b)
        return;
    for (int j = b * (b + 1) / 2 + 1; j <= (b + 1) * (b + 2) / 2; j ++)
        printf("%4.d", j);
    printf("\n");
    return floydTriangleRecursive(a, b + 1);
}
 
int main(void)
{
    int n;
    printf("Enter the number of rows: ");
    scanf("%d", &n);
    floydTriangleRecursive(n, 0);
    return 0;
}
Program Explanation

1. The program asks the user to enter the number of rows and save the value in a n variable and returns a Floyd triangle with the same height.
2. The recursive function accepts two parameters namely, the total rows and the current row.
3. The starting element of the current row would then be the sum of elements of the previous rows.
4. The ending element would similarly be just less than the sum of elements of all the rows upto the current row.

Time complexity: O(n)
The time complexity of displaying the Floyd triangle is O(n).

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, the entered value is “4” to print the floyds triangle.

Enter the number of rows: 4
   1 
   2    3 
   4    5    6 
   7    8    9   10

Testcase 2: In this case, the entered value is “10” to print the floyds triangle.

Enter the number of rows: 10
   1 
   2    3 
   4    5    6 
   7    8    9   10 
  11   12   13   14   15 
  16   17   18   19   20   21 
  22   23   24   25   26   27   28 
  29   30   31   32   33   34   35   36 
  37   38   39   40   41   42   43   44   45 
  46   47   48   49   50   51   52   53   54   55

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.