Repeated Number Triangle in C++

What You’ll Learn
How to print the repeated number triangle pattern 1, 22, 333, 4444, 55555 in C++ using nested for loops.
The trick is printing the row index i inside the inner loop, so each row repeats the same digit.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
22
333
4444
55555Complete C++ Program
Fixed five rows: each row prints the same number (i) exactly i times.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
cout << i;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the row count
int rows = 5; sets how many lines will be printed.
Outer loop (row value)
for (i = 1; i <= rows; i++) chooses which digit is repeated on that row. When i is 4, the row is made of 4s.
Inner loop (repeat i)
for (j = 1; j <= i; j++) runs i times. Printing cout << i repeats the same digit across the row.
New line
cout << "\n"; moves to the next row after each line.
Repeated number 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 = 1; i <= rows; i++) {
for (j = 1; 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 << ' ' - Invert the pattern by running the outer loop from
rowsdown to 1 - Print a different symbol per row (letters, stars) using the same loop structure
- 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 - Printing
jinstead ofi(that becomes the ascending triangle pattern) - Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop chooses the digit to repeat on that row.
The inner loop repeats the digit i exactly i times.
Total printed digits follow the triangular number count: n(n+1)/2.
The only change from an ascending triangle is printing i instead of j.
❓ Frequently Asked Questions
i repeats the row number on the same line. If you print j, you get 1, 12, 123, ... which is a different pattern.cout << i << ' '; inside the inner loop.for (i = rows; i >= 1; i--) and keep printing i inside the inner loop.Explore More C++ Number Patterns!
Once you master this, try mixing repeated digits with spacing and alignment.
This pattern is a classic way to practice nested loops: the inner loop length depends on the outer loop index. It’s the same structure behind many triangles and pyramids.
12 people found this page helpful
