Descending Reverse Number Triangle in C++

What You’ll Learn
How to print the descending reverse number triangle pattern 54321, 4321, 321, 21, 1 in C++ using nested for loops.
The outer loop reduces the row length, and the inner loop counts backward from the current row limit down to 1.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
54321
4321
321
21
1Complete C++ Program
Fixed five rows: the outer loop counts down; the inner loop prints from i down to 1 on each row.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--) {
for (j = i; j >= 1; j--) {
cout << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the row count
int rows = 5; sets the maximum number and how many lines will be printed.
Outer loop (descending rows)
for (i = rows; i >= 1; i--) reduces the length of each line from rows down to 1.
Inner loop (print i..1)
for (j = i; j >= 1; j--) prints the reverse sequence on the current row using cout << j.
New line
cout << "\n"; completes the row and moves to the next line.
Reverse 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 decide the maximum number (and the 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 = rows; i >= 1; i--) {
for (j = i; j >= 1; 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 << ' ' - Print the ascending version by changing the inner loop to count up from 1 to
i - Right-align the triangle by printing leading spaces before each row
- Replace numbers with characters to create alphabet patterns
Avoid
- Forgetting to print a newline between rows
- Using negative or zero rows without validating input
- Mixing row/column logic (keep the outer loop for rows)
- Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop decreases the row length from rows down to 1.
The inner loop prints a reverse sequence from i down to 1 on each row.
Total printed digits follow the triangular number count: n(n+1)/2.
This is a great stepping stone to patterns like Program 1 (ascending digits) and inverted/shifted triangles.
❓ Frequently Asked Questions
for (j = i; j >= 1; j--). That makes the row print i, i-1, ..., 1.12345, 1234, 123, ... (ascending on each row). This program prints 54321, 4321, 321, ... (descending on each row).cout << j; with cout << j << ' ';.Explore More C++ Number Patterns!
Try more variations to strengthen your nested-loop skills.
You can generate many number patterns by changing just two things: the start and end values of the inner loop, and whether it counts up or down.
12 people found this page helpful
