Online Snippets Library

Create Floyd's triangle in C

Floyd's triangle is a right-angled triangular array of natural numbers. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

1 2 3 4 5 6 7 8 9 10 C Program
#include <stdio.h>
int main() {
   int n,i,j,k=1;

   n = 4;

   for(i = 1; i <= n; i++) {
      for(j=1;j <= i; j++)
         printf("%3d", k++);

      printf("\n");
   }
   
   return 0;
}

Output

1 2 3 4 5 6 7 8 9 10