Floyd's triangle in C language
The Floyd's triangle is a left aligned or right-angled triangle that contains consecutive natural numbers starting from 1 still the (N * N+1) / 2 where N is number of rows.
Floyd's triangle
 1 N = 5 (5 * 6 / 2 = 15)
 2 
 3  1 
 4  2  3 
 5  4  5  6 
 6  7  8  9 10 
 7 11 12 13 14 15
Floyd's triangle
 1 #include <stdio.h>
 2 
 3 int main() {
 4     
 5     int i, j, rows, n = 1;
 6     printf("Enter number of rows for floyd's triangle : ");
 7     scanf("%d", &rows);
 8     printf("Floyd's Triangle !! \n");
 9     
 10     for(i = 0; i < rows; i++) {
 11         for(j = 0; j <= i; j++) {
 12             printf("%2d ", n++);
 13         }
 14         printf("\n");
 15     }
 16     
 17     return 0;
 18 }
In the above example, we are accepting input from user which is number of rows. Using nested for loop we are printing triangle using variable n, where n is initialize with value 1 and continue until number of rows will be printed. In each nested iteration value of n is incremented by 1.
Output
 1 Enter number of rows for floyd's triangle : 5
 2 Floyd's Triangle !! 
 3  1 
 4  2  3 
 5  4  5  6 
 6  7  8  9 10 
 7 11 12 13 14 15 
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us