Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to print Floyd's Triangle

C Code Example — Pattern Programs

ADVERTISEMENT

C Program to print Floyd's Triangle

Objective

Write a C program to print Floyd's Triangle.

Algorithm / Approach

  1. Declare a counter variable a = 1.
  2. Run an outer loop i from 1 to n rows.
  3. Run an inner loop j from 1 to i.
  4. Inside the inner loop, print a and immediately increment it (a++).
  5. Print a newline.
main.c
#include<stdio.h>
int main( ) {
 int a = 1, i, j, n;
 printf("Enter the number of rows ");
 scanf("%d",&n);
 for(i = 1; i<=n; i++) {
  for(j = 1; j<=i; j++) {
   printf("%d",a);
   a++;
  }
  printf("\n");
 }
 return 0;
}

Expected Output

1
2 3
4 5 6
7 8 9 10

Explanation of the Program

  • Floyd's Triangle is a right-angled triangular array of natural numbers.
  • Unlike previous patterns where we printed the loop variables (i or j), here we print a completely independent counter (a) that never resets. It simply keeps counting upwards forever as the loops run.

Complexity

Time Complexity O(n^2)
Space Complexity O(1)
ADVERTISEMENT