OverIQ.com

C Program to print Floyd’s Triangle

Last updated on September 24, 2020


Floyd's Triangle looks like this:

1
2
3
4
5
6
1     
2     3     
4     5     6     
7     8     9     10    
11    12    13    14    15    
16    17    18    19    20    21

The following is a C program to print Floyd's triangle:

 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
/***************************************
 * C Program to print Floyd's Triangle
 ***************************************/

#include<stdio.h> // include stdio.h

int main() 
{
    int n, k = 1;

    printf("Enter number of lines: ");
    scanf("%d", &n);

    printf("\n");       

    // loop for number of lines    
    for(int i = 1; i <= n; i++)
    {
        //loop  to print numbers in each line
        for(int j = 1; j <= i; j++)
        {            
            printf("%-5d ", k++);            
        }

        printf("\n");
    }  

    return 0;
}

Try it now

Expected Output:

1
2
3
4
5
6
7
Enter number of lines: 5

1     
2     3     
4     5     6     
7     8     9     10    
11    12    13    14    15

Recommended Reading: