Left-Aligned Descending Number Triangle in C++

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:
54321
5432
543
54
5Complete C++ Program
Fixed five rows: the inner loop prints from rows down to the current row index i.
#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
Choose the maximum number
int rows = 5; sets the maximum number and the number of rows.
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.
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.
New line
cout << "\n"; completes each row.
Left-aligned descending triangle
Total numbers printed: n + (n-1) + … + 1 = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user choose the maximum number (and number of rows) using cin:
#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 usingrows - 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
5everywhere instead of usingrows - Printing from
idown to 1 (that becomes Program 3’s shape) - Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop moves the stopping point forward (from 1 to rows) to shorten each line.
The inner loop always starts at rows and counts down to the current row index i.
Total printed digits follow the triangular number count: n(n+1)/2.
Small loop-bound changes produce very different patterns (compare Program 3 vs Program 4).
❓ Frequently Asked Questions
j = rows. Only the ending condition changes, so each row starts with the maximum number.54321, then 4321, then 321 (the first number changes). Program 4 prints 54321, then 5432, then 543 (the first number stays the same).cout << j << ' '; in the inner loop.Explore More C++ Number Patterns!
Play with loop bounds to create dozens of variations.
Program 4 is a good example of how changing only the inner loop start (from 1 to rows) can transform the entire pattern output.
12 people found this page helpful
