Reverse Growing Number Pattern in C++

What You’ll Learn
How to print the reverse growing number pattern 5, 54, 543, 5432, 54321 in C++ using nested for loops.
This pattern grows by one digit each row while staying in descending order within each row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
54
543
5432
54321Complete C++ Program
Fixed five rows: inner loop prints rows..i; as i decreases, the row grows.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; 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 digit (5) and how many lines you print.
Outer loop (grow the row)
for (i = rows; i >= 1; i--) decreases i. A smaller i means the inner loop runs longer, so the row gains one extra digit each time.
Inner loop (print rows..i)
for (j = rows; j >= i; j--) prints descending digits starting from rows down to i. That yields 5, then 54, then 543, etc.
New line
cout << "\n"; completes each row.
Reverse growing pattern
Total digits printed: 1+2+…+n = 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 = 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 << ' ' - Print the non-reverse growing version (
1,12, ...) by counting up in the inner loop - Try printing letters instead of numbers to get an alphabet version
- Right-align the triangle by printing leading spaces before each row
Avoid
- Forgetting to print a newline between rows
- Hard-coding
5everywhere instead of usingrows - Mixing row/column logic (keep the outer loop for rows)
- Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop decreases the inner-loop lower bound, which makes each row longer.
The inner loop prints descending numbers from rows to i.
Total printed digits follow the triangular number count: n(n+1)/2.
Small loop-bound tweaks are enough to switch between shrinking and growing patterns.
❓ Frequently Asked Questions
i. When i is smaller, the condition j >= i stays true for more values of j, so the inner loop prints more digits.cout << j << ' '; inside the inner loop.rows..i). The difference is the outer loop direction: Program 4 increases i (shrinking), while Program 8 decreases i (growing).Explore More C++ Number Patterns!
Small changes in loop bounds can generate many pattern styles.
You can think of this pattern as building a string by prepending one digit each row—but loops let you generate it without any string operations.
12 people found this page helpful
