Right-Aligned Floyd’s Triangle in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Formatting (setw)

What You’ll Learn

How to print a right-aligned triangle of consecutive numbers in C++:

1, then 2 3, then 4 5 6, continuing until the last row.

We’ll use nested loops and setw() from <iomanip> to keep the triangle aligned.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C++ Program

The inner loop prints either spaces (for alignment) or the next number using setw(3).

C++
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int rows = 5;
    int i, j;
    int k = 1;

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= 1; j--) {
            if (j > i) {
                cout << "   ";
            } else {
                cout << setw(3) << k++;
            }
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Initialize row count and counter

rows = 5 sets the triangle height and k = 1 is the next number to print.

Setup
2

Outer loop prints rows

for (i = 1; i <= rows; i++) controls how many numbers appear on each line (row i prints i numbers).

Row control
3

Inner loop handles alignment

for (j = rows; j >= 1; j--) prints either padding spaces (when j > i) or a number (otherwise).

Alignment
4

setw(3) keeps columns neat

setw(3) reserves 3 characters for each number so 1-digit and 2-digit values align correctly.

Formatting
=

Right-aligned Floyd’s triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide how many rows to print using cin:

C++
#include <iostream>
#include <iomanip>
using namespace std;

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= 1; j--) {
            if (j > i) {
                cout << "   ";
            } else {
                cout << setw(3) << k++;
            }
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Increase setw() when printing larger numbers (e.g., 100+)
  • Reset k each row to print repeated-row patterns instead of consecutive numbers
  • Turn it into a left-aligned Floyd’s triangle by printing only the i numbers and skipping the padding loop
  • Use std::right or std::left for different alignment effects
  • Print the same triangle with a custom start value (initialize k to something else)

Avoid

  • Using endl inside loops (it flushes output and slows printing)
  • Hard-coding the width if the pattern size can grow a lot
  • Mixing alignment and value logic in the same loop without a clear condition
  • Forgetting to increment k after printing a number

Key Takeaways

1

Use a counter k to print consecutive integers across rows.

2

Right alignment comes from printing spaces while j > i.

3

setw(3) keeps the triangle aligned for multi-digit values.

4

Total printed values are n(n+1)/2, giving O(n²) time.

❓ Frequently Asked Questions

Because each number is printed with setw(3). Printing " " keeps the padding width consistent with the number width.
Increase the width passed to setw() (for example, use setw(4) or setw(5)) when numbers can reach 1000+.
Yes. You can print leading spaces based on rows - i first, then print the i numbers. This example keeps alignment and printing in one inner loop.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C++ Number Patterns!

Try combining alignment rules with different value formulas to build new triangle patterns.

All Number Patterns →
Did you know?

setw() doesn’t change your number—it only changes how it is displayed. That makes it perfect for pattern printing where alignment matters.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful