Increasing Suffix Number Pattern in C++

What You’ll Learn
How to print the increasing suffix number pattern 5, 45, 345, 2345, 12345 in C++ using nested for loops.
The outer loop counts down the starting point, and the inner loop prints upward from i to rows.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
45
345
2345
12345Complete C++ Program
Fixed five rows: outer loop moves the starting number left; inner loop prints from i up to rows.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--) {
for (j = i; j <= rows; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the maximum number
int rows = 5; sets the maximum number and total rows.
Outer loop (start value)
for (i = rows; i >= 1; i--) moves the start from rows down to 1. Each next row starts one number earlier.
Inner loop (print i..rows)
for (j = i; j <= rows; j++) prints from the current start i up to rows using cout << j.
New line
cout << "\n"; completes each row before moving on.
Increasing suffix pattern
Total numbers 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 = i; j <= rows; 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 << ' ' - Try the opposite version by making the start increase (see Program 2)
- Print the same pattern with repeated digits by printing
rowsinstead ofj - 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 - Starting the inner loop at 1 (that would create a different triangle)
- Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop moves the start value from rows down to 1.
The inner loop prints numbers from i up to rows on each line.
Total printed digits follow the triangular number count: n(n+1)/2.
This is the mirror idea of Program 2: one removes the left side, the other builds it.
❓ Frequently Asked Questions
i equals rows, so the inner loop prints only rows (just 5). Each next row starts one value earlier, so the line grows.12345, then 2345, then 345 (it removes digits from the left). Program 6 prints 5, then 45, then 345 (it adds digits on the left).cout << j << ' '; inside the inner loop.Explore More C++ Number Patterns!
Keep going to learn more number patterns and loop tricks.
This pattern is a great way to practice thinking about loop bounds: the outer loop controls the start, and the inner loop controls the end.
12 people found this page helpful
