0
0
CProgramBeginner · 2 min read

C Program to Print Floyd Triangle with Output and Explanation

To print Floyd's triangle in C, use nested for loops where the outer loop controls rows and the inner loop prints consecutive numbers, incrementing a counter each time, like for(i=1; i<=rows; i++){ for(j=1; j<=i; j++){ printf("%d ", num++); }}.
📋

Examples

Input3
Output1 2 3 4 5 6
Input5
Output1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Input1
Output1
🧠

How to Think About It

To print Floyd's triangle, think of it as rows of numbers where each row has one more number than the previous. Start with 1 and keep increasing the number as you print. Use one loop to go through each row and another loop inside it to print the numbers in that row.
📐

Algorithm

1
Get the number of rows from the user.
2
Initialize a counter variable to 1.
3
For each row from 1 to the number of rows:
4
Print numbers equal to the current row number, incrementing the counter each time.
5
Move to the next line after each row.
💻

Code

c
#include <stdio.h>

int main() {
    int rows, num = 1;
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("%d ", num++);
        }
        printf("\n");
    }
    return 0;
}
Output
Enter number of rows: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
🔍

Dry Run

Let's trace the program with input 3 to see how Floyd's triangle is printed.

1

Initialize variables

rows = 3, num = 1

2

First row (i=1)

Print 1 number: print 1, num becomes 2

3

Second row (i=2)

Print 2 numbers: print 2, 3, num becomes 4

4

Third row (i=3)

Print 3 numbers: print 4, 5, 6, num becomes 7

Row (i)Numbers PrintedValue of num after row
112
22 34
34 5 67
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to the number of rows, deciding how many rows to print.

Step 2: Inner loop prints numbers per row

The inner for loop runs as many times as the current row number, printing numbers and increasing the counter.

Step 3: Counter increments after each print

The variable num starts at 1 and increases by 1 after printing each number, ensuring consecutive numbers.

🔄

Alternative Approaches

Using while loops
c
#include <stdio.h>

int main() {
    int rows, i = 1, j, num = 1;
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    while (i <= rows) {
        j = 1;
        while (j <= i) {
            printf("%d ", num++);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}
This uses while loops instead of for loops; logic is the same but syntax differs.
Using a single loop with manual line breaks
c
#include <stdio.h>

int main() {
    int rows, num = 1, count = 0, i = 1;
    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for (int k = 1; k <= rows*(rows+1)/2; k++) {
        printf("%d ", num++);
        count++;
        if (count == i) {
            printf("\n");
            i++;
            count = 0;
        }
    }
    return 0;
}
This uses one loop and tracks when to break lines, which is less intuitive but reduces nested loops.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times cumulatively, resulting in O(n^2) time.

Space Complexity

Only a few integer variables are used, so space complexity is O(1), constant space.

Which Approach is Fastest?

All approaches have similar O(n^2) time; using for loops is clearer and preferred for readability.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clarity and simplicity
Nested while loopsO(n^2)O(1)Alternative syntax preference
Single loop with manual breaksO(n^2)O(1)Reducing nested loops but less readable
💡
Use nested loops with a counter variable to print Floyd's triangle easily.
⚠️
Beginners often forget to increment the number counter inside the inner loop, causing repeated numbers.