There are number of ways to display and Floyd triangle display consecutive numbers in different row. In this example, you will write a program to print Floyd triangle.
Before you begin, learn the basics of C programming language. If you know the basics, skip and continue reading.
- C Program Structure.
- How to install Turbo C++ compiler?
- C Printing Outputs.
- C Reading Input Values.
- C For Loop.
Problem Definition
The program ask for number of row to print. When the user input the number, a list of consecutive numbers are printed for each row. The number of elements for each row also grows.
Suppose the user input is 5 , then five rows of consecutive number are printed and each row has one more number than the previous. See the figure below.
Program Source Code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,c,a=1;
printf("Enter the Number of Rows for Floyd Triangle:\n");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
printf("%d\t",a);
a++;
}
printf("\n");
}
getch();
return 0;
system("PAUSE");
return 0;
}
Output
Enter the Number of Rows for Floyd Triangle:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15