Floyd’s Triangle in C – Nested Loops with Counter

Floyd’s triangle in C is a right-angled triangular arrangement of consecutive natural numbers. Row 1 contains 1 number, row 2 contains 2, row 3 contains 3, and so on. It is named after Robert W. Floyd, who used it to teach loop control and nested iteration. The total numbers through row n is 1 + 2 + 3 + … + n = n(n+1)/2.

How It Works — Step by Step

Floyd’s triangle for 5 rows:

   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15

The logic uses two nested loops and a single counter k that keeps incrementing across all rows:

Row (i) Numbers printed Count of numbers k range
1 1 1 1
2 2, 3 2 2–3
3 4, 5, 6 3 4–6
4 7, 8, 9, 10 4 7–10
5 11, 12, 13, 14, 15 5 11–15

C Program for Floyd’s Triangle

/* Floyd's triangle in C
 * Compile: gcc -ansi -Wall -Wextra floyd.c -o floyd */
#include <stdio.h>

int main(void)
{
    int i, j, n, k = 1;

    printf("Enter the number of rows: ");
    scanf("%d", &n);

    if (n <= 0) {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    printf("\nFloyd's triangle (%d rows):\n\n", n);
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++, k++)
            printf("%4d", k);
        printf("\n");
    }

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra floyd.c -o floyd
./floyd

Sample Input and Output

Enter the number of rows: 5

Floyd's triangle (5 rows):

   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15
Enter the number of rows: 7

Floyd's triangle (7 rows):

   1
   2   3
   4   5   6
   7   8   9  10
  11  12  13  14  15
  16  17  18  19  20  21
  22  23  24  25  26  27  28

Code Explanation

  • int k = 1 — the running counter that starts at 1 and increments every time a number is printed, regardless of which row or column we are in. It is declared outside both loops so it persists across all rows.
  • for (i = 1; i <= n; i++) — outer loop controls the row. Row number i has exactly i elements.
  • for (j = 1; j <= i; j++, k++) — inner loop runs i times for row i. The j++, k++ update expression increments both j (the column counter) and k (the number to print) simultaneously using the comma operator.
  • printf(“%4d”, k) — field width 4 aligns the numbers in columns. Without this, a single space would cause misalignment once k reaches 2 digits (10, 11, …).
  • printf(“\n”) after the inner loop — moves to the next line after all numbers in the current row are printed.

Why k Lives Outside Both Loops

If k were declared inside the outer loop (for (i=1; i<=n; i++) { int k=...; }), it would reset to 1 at the start of each row, printing 1, 1 2, 1 2 3, … — a Pascal’s triangle left-column effect, not Floyd’s triangle. Placing k outside both loops gives it the lifetime of the entire function, so it accumulates across all rows.

Useful Formulas

  • Total numbers in n rows = 1 + 2 + … + n = n(n+1)/2
  • First number in row r = (r−1)(r−2)/2 + 1 = triangular number of (r−1) + 1
  • Last number in row r = r(r+1)/2

For row 5: first = (4×3)/2 + 1 = 7? Wait: (5-1)×(5-2)/2 + 1 = 4×3/2 + 1 = 6 + 1 = 7. But the output shows row 5 starting at 11. Let me recalculate: rows 1–4 contain 1+2+3+4 = 10 numbers, so row 5 starts at 11. The formula: first in row r = 1 + 2 + … + (r−1) + 1 = (r−1)r/2 + 1. For r=5: 4×5/2 + 1 = 10 + 1 = 11 ✓.

What This Program Teaches

  • Nested loops with an external counter — the key insight is that k is controlled by the inner loop but lives at the outer scope. This pattern appears wherever you need a globally-incrementing counter across a nested iteration.
  • Comma operator in for-loop updatej++, k++ increments two variables in a single update expression, a common idiom for parallel counters in the same loop.
  • Triangular numbers — Floyd’s triangle visually demonstrates the triangular number sequence: 1, 3, 6, 10, 15, 21, 28, … (cumulative row totals).
  • printf field width for alignment%4d reserves 4 characters per number. Right-aligned numbers stay in neat columns even as they grow to 2 or 3 digits.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>