Write a C program to print the Floyd’s triangle number pattern using loop. How to print the Floyd’s triangle number pattern using for loop in C programming. Logic to print the Floyd’s triangle number pattern using loop in C program.
Required knowledge
Logic to print the Floyd’s triangle number pattern
Printing the above pattern is easy if you are acquainted with basics of number pattern printing. The given pattern contains N rows (where N is the total number of rows to be printed). Each row contains exactly i columns (where i is the current row number).
Now, you have understood the looping constructs of the given pattern. As you can see that printed numbers are in increasing order, hence to print numbers we will use an extra variable k which will keep track of the next number to be printed.
Step-by-step descriptive logic:
- Initialize a variable k = 1 (where k is used to keep track of next number to be printed).
- To iterate through rows, run an outer loop from 1 to N.
- To print columns, run an inner loop from 1 to i (where i is the current row number).
Inside this loop print the value of k also increment the value of k to k = k + 1.
Program to print the Floyd’s triangle number pattern
/**
* C program to print Floyd number pattern
*/
#include <stdio.h>
int main()
{
int i, j, k, N;
printf("Enter N: ");
scanf("%d", &N);
k = 1;
for(i=1; i<=N; i++)
{
// Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%3d", k);
}
printf("\n");
}
return 0;
}
Output
Enter N: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Happy coding 😉