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;
}
|
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:
- C Program to calculate the difference of two dates in years, months and days
- C Program to calculate the day of year from the date
- C Program to print the date in legal form
- C Program to print various triangular patterns
- C Program to print Pascal Triangle
Load Comments