Ascending Repeating Shrinking Pattern in C++

What You’ll Learn
How to print the number pattern 11111, 2222, 333, 44, 5 in C++ using nested for loops.
The idea is simple: the row digit increases (1, 2, 3, ...), while the number of repeats decreases because the inner loop starts at j = i.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
11111
2222
333
44
5Complete C++ Program
The outer loop increases from 1 to rows. The inner loop runs from i to rows and prints i, creating fewer repeats on each next line.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = i; j <= rows; j++) {
cout << i;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Set the size
int rows = 5; sets the height of the pattern and the maximum digit.
Outer loop (digit per row)
for (i = 1; i <= rows; i++) picks the digit to print on each row: 1, 2, 3, 4, 5.
Inner loop (shrink repeats)
for (j = i; j <= rows; j++) runs fewer times as i grows. Each iteration prints i, so the row becomes i repeated.
New line
cout << "\n"; moves to the next row after printing each line.
Shrinking repeats
Total digits printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read rows from the user so the pattern size can be changed at runtime:
#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 = i; j <= rows; 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 using
cout << i << ' '; - Right-align the pattern by printing leading spaces before each row
- Print the decreasing-digit version by running the outer loop from
rowsdown to 1 - Switch to characters to create alphabet patterns
Avoid
- Forgetting to print a newline after each row
- Hard-coding
5throughout (userowsinstead) - Using
endlinside loops (it flushes output) - Not handling
rows <= 0if you accept user input
Key Takeaways
The outer loop chooses the digit per row (1, 2, 3, ...).
The inner loop controls the row length by starting at j = i.
This produces repeated digits with shrinking width: 11111 to 5.
Total prints still follow the triangular count: n(n+1)/2.
❓ Frequently Asked Questions
j = i. When i increases, the loop runs fewer times, so each line contains fewer digits.i (not j) inside the inner loop. That makes each row a repetition of the same digit.j inside the inner loop: cout << j;. That changes the output to a shifting sequence pattern.Explore More C++ Number Patterns!
Try more repeating and non-repeating patterns to master nested loops.
Starting the inner loop from j = i is a common trick to create shrinking patterns without extra counters.
12 people found this page helpful
