Incrementing Start Number Pattern in C++

What You’ll Learn
How to print the number pattern 12345, 2345, 345, 45, 5 in C++ using nested for loops.
The key idea is that the inner loop begins at the current outer loop index (j = i), so each row starts one number later than the previous row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
12345
2345
345
45
5Complete C++ Program
Fixed five rows: the outer loop sets the starting number on each row; the inner loop prints from that start up to rows.
#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 << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Set the maximum number
int rows = 5; defines both the height and the maximum number printed on each line.
Outer loop (row start)
for (i = 1; i <= rows; i++) moves the starting number from 1 to rows. Each increment of i shifts the row left by removing the first number.
Inner loop (print i..rows)
for (j = i; j <= rows; j++) prints from the row’s starting number i up to rows using cout << j.
New line
cout << "\n"; completes a row before moving to the next.
Shifted number pattern
Total numbers printed: n + (n-1) + … + 1 = 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 = 1; i <= rows; 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 << ' 'for readability - Print each row on one line using
std::stringaccumulation (useful for formatting) - Start from a different base number (e.g., 10..n) by adjusting what you print inside the loop
- Try the reverse idea: keep start fixed at 1 and decrease the end each row (Program 1)
Avoid
- Forgetting to print a newline between rows
- Hard-coding
5everywhere instead of usingrows - Printing from
1in the inner loop (that would repeat 12345 on every row) - Flushing output with
endlunnecessarily in tight loops
Key Takeaways
The outer loop sets the starting number for each row (i = 1..rows).
The inner loop prints numbers from i to rows, so each row becomes shorter.
Total printed digits follow the triangular number count: n(n+1)/2.
This is the same idea as slicing a sequence—just with loops and console output.
❓ Frequently Asked Questions
j = i. As i increases (1, 2, 3, ...), the first printed number shifts forward, producing 12345, then 2345, then 345, etc.cout << j << ' ';. You can also trim the trailing space if needed.rows using cin and use it as the loop bound. The last printed line will then contain just rows.Explore More C++ Number Patterns!
Continue practicing nested loops with more number patterns.
This pattern prints the same total number of digits as other triangular patterns: n(n+1)/2. Only the starting value changes from row to row.
12 people found this page helpful
