0
0
CppProgramBeginner · 2 min read

C++ Program to Print Floyd Triangle

You can print Floyd's triangle in C++ using nested for loops and a counter variable, for example: int num = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { std::cout << num++ << " "; } std::cout << std::endl; }.
📋

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 printing numbers in rows where the first row has 1 number, the second has 2, and so on. Use a counter starting at 1 and increase it after printing each number. Use two loops: the outer loop for rows and the inner loop for numbers in each row.
📐

Algorithm

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

Code

cpp
#include <iostream>

int main() {
    int n, num = 1;
    std::cout << "Enter number of rows: ";
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            std::cout << num++ << " ";
        }
        std::cout << std::endl;
    }
    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 for input n=3 through the code.

1

Initialize variables

n = 3, num = 1

2

First row (i=1)

Print 1 number: print num=1, then num=2

3

Second row (i=2)

Print 2 numbers: print num=2, num=3, then num=4

4

Third row (i=3)

Print 3 numbers: print num=4, num=5, num=6, then num=7

Row (i)Numbers Printednum after row
112
22 34
34 5 67
💡

Why This Works

Step 1: Outer loop controls rows

The for loop with variable i runs from 1 to n, controlling how many rows to print.

Step 2: Inner loop prints numbers per row

The inner for loop runs i times to print the correct count of numbers in each row.

Step 3: Counter increments after each print

The variable num starts at 1 and increases by 1 after printing each number to continue the sequence.

🔄

Alternative Approaches

Using while loops
cpp
#include <iostream>

int main() {
    int n, num = 1, i = 1;
    std::cout << "Enter number of rows: ";
    std::cin >> n;
    while (i <= n) {
        int j = 1;
        while (j <= i) {
            std::cout << num++ << " ";
            j++;
        }
        std::cout << std::endl;
        i++;
    }
    return 0;
}
This uses <code>while</code> loops instead of <code>for</code> loops, which some beginners find easier to understand.
Using recursion
cpp
#include <iostream>

void printRow(int count, int &num) {
    if (count == 0) return;
    std::cout << num++ << " ";
    printRow(count - 1, num);
}

void printFloyd(int row, int n, int &num) {
    if (row > n) return;
    printRow(row, num);
    std::cout << std::endl;
    printFloyd(row + 1, n, num);
}

int main() {
    int n, num = 1;
    std::cout << "Enter number of rows: ";
    std::cin >> n;
    printFloyd(1, n, num);
    return 0;
}
This recursive approach is elegant but less intuitive for beginners and uses function calls instead of 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 the space complexity is O(1), constant space.

Which Approach is Fastest?

The loop-based approach is fastest and simplest. The recursive method adds overhead due to function calls.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and efficient
Nested while loopsO(n^2)O(1)Beginners preferring while loops
RecursionO(n^2)O(n)Learning recursion, less efficient
💡
Use a single counter variable that increases after printing each number to keep the sequence continuous.
⚠️
Forgetting to increment the counter variable after printing each number causes repeated numbers in the triangle.