C Program To Print Floyd Triangle Pattern
Last Updated :
23 Nov, 2022
Here, we will build a C Program To Print Floyd Triangle Pattern using 2 approaches i.e.
- Using for loop
- Using while loop
Input:
n = 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1. Using for loop
The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the number and increment the number to print the next number.
C
#include <stdio.h>
int main()
{
int rows, columns, number = 1, n = 5;
for (rows = 0; rows <= n; rows++) {
for (columns = 0; columns < rows; columns++) {
printf ( "%d " , number);
number++;
}
printf ( "\n" );
}
return 0;
}
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
2. Using while loop:
The while loops check the condition until the condition is false. If the condition is true then enters into the loop and execute the statements.
C
#include <stdio.h>
int main()
{
int rows = 1, columns = 0, n = 5;
int number = 1;
while (rows <= n) {
while (columns <= rows - 1) {
printf ( "%d " , number);
columns++;
number++;
}
columns = 0;
rows++;
printf ( "\n" );
}
return 0;
}
|
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Share your thoughts in the comments
Please Login to comment...