Reverse Repeated Number Triangle in C++

What You’ll Learn
How to print the reverse repeated number triangle pattern 5, 44, 333, 2222, 11111 in C++ using nested for loops.
Each row repeats the same digit, but the digit decreases while the count increases.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
44
333
2222
11111Complete C++ Program
Fixed five rows: outer loop chooses the digit; inner loop repeats it rows-i+1 times.
#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 << i;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the row count
int rows = 5; defines how many lines to print.
Outer loop (digit)
for (i = rows; i >= 1; i--) chooses the digit printed on each row (5, then 4, then 3...).
Inner loop (repeat)
for (j = rows; j >= i; j--) controls how many times the digit repeats. When i is smaller, the loop runs more times, so the row gets longer.
New line
cout << "\n"; moves to the next row.
Reverse repeated-digit triangle
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 number of rows at runtime 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 << i;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate input (e.g., check
cin.fail()) before usingrows - Add spaces between digits with
cout << i << ' ' - Switch to ascending repeated digits by looping
ifrom 1 torows(Program 9) - Try right-aligning by printing spaces before each row
- Use characters instead of digits to make an alphabet variant
Avoid
- Forgetting to print a newline between rows
- Hard-coding
5everywhere instead of usingrows - Printing
jinstead ofi(that prints a different pattern) - Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop selects the digit and counts down from rows to 1.
The inner loop controls the row length by running from rows down to i.
Total printed digits follow the triangular number count: n(n+1)/2.
This is the reverse counterpart of Program 9 (ascending repeated-digit triangle).
❓ Frequently Asked Questions
i is smaller, the condition j >= i stays true for more values of j, so the inner loop runs more times. That increases the number of repeats.i increasing from 1 to rows. Here, i decreases from rows to 1, producing the reverse order of repeated digits.cout << i << ' '; inside the inner loop.Explore More C++ Number Patterns!
Try combining this repeated-digit idea with alignment and spacing for more complex patterns.
This pattern is a neat example of two moving pieces: the digit decreases each row, but the repeat count increases. It comes from changing the inner loop bound to depend on i.
12 people found this page helpful
