Left-Aligned Descending Number Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a left-aligned descending number triangle (54321, 5432, 543, 54, 5) in C++ using nested for loops.

Each row starts at the maximum number (rows) and the row becomes shorter by increasing the stopping condition of the inner loop.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
5432
543
54
5
1

Complete C++ Program

Fixed five rows: the inner loop prints from rows down to the current row index i.

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

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

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= i; j--) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Choose the maximum number

int rows = 5; sets the maximum number and the number of rows.

Setup
2

Outer loop (row index)

for (i = 1; i <= rows; i++) increases the row index. As i grows, the inner loop stops earlier—so each next line has one fewer digit.

Row control
3

Inner loop (print rows..i)

for (j = rows; j >= i; j--) prints from the maximum down to the current row index, producing 54321, then 5432, then 543, and so on.

Number printing
4

New line

cout << "\n"; completes each row.

Line break
=

Left-aligned descending triangle

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

2

Variation — User Input Version

Let the user choose the maximum number (and number of rows) using cin:

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

int main() {
    int rows;
    int i, j;

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

    for (i = 1; i <= rows; i++) {
        for (j = rows; j >= i; j--) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Add spaces between numbers with cout << j << ' '
  • For a right-aligned look in the console, pad shorter rows with leading spaces before the digits
  • Try the sibling pattern where the row starts changes (see Program 3)
  • Convert this to a star pattern by printing * instead of numbers

Avoid

  • Forgetting to print a newline between rows
  • Hard-coding 5 everywhere instead of using rows
  • Printing from i down to 1 (that becomes Program 3’s shape)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop moves the stopping point forward (from 1 to rows) to shorten each line.

2

The inner loop always starts at rows and counts down to the current row index i.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

Small loop-bound changes produce very different patterns (compare Program 3 vs Program 4).

❓ Frequently Asked Questions

Because the inner loop always begins at j = rows. Only the ending condition changes, so each row starts with the maximum number.
Program 3 prints 54321, then 4321, then 321 (the first number changes). Program 4 prints 54321, then 5432, then 543 (the first number stays the same).
Yes. Print cout << j << ' '; in the inner loop.
O(n²) for n rows: you print n+(n-1)+…+1 = n(n+1)/2 numbers.

Explore More C++ Number Patterns!

Play with loop bounds to create dozens of variations.

All Number Patterns →
Did you know?

Program 4 is a good example of how changing only the inner loop start (from 1 to rows) can transform the entire pattern output.

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