Incrementing Start Number Pattern in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
12345
2345
345
45
5
1

Complete 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.

C++
#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

1

Set the maximum number

int rows = 5; defines both the height and the maximum number printed on each line.

Setup
2

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.

Row control
3

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.

Number printing
4

New line

cout << "\n"; completes a row before moving to the next.

Line break
=

Shifted number pattern

Total numbers printed: n + (n-1) + … + 1 = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the maximum number (and the number of rows) using cin:

C++
#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 using rows
  • Add spaces between numbers with cout << j << ' ' for readability
  • Print each row on one line using std::string accumulation (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 5 everywhere instead of using rows
  • Printing from 1 in the inner loop (that would repeat 12345 on every row)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop sets the starting number for each row (i = 1..rows).

2

The inner loop prints numbers from i to rows, so each row becomes shorter.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

This is the same idea as slicing a sequence—just with loops and console output.

❓ Frequently Asked Questions

Because the inner loop starts at j = i. As i increases (1, 2, 3, ...), the first printed number shifts forward, producing 12345, then 2345, then 345, etc.
Inside the inner loop, print a space after each number: cout << j << ' ';. You can also trim the trailing space if needed.
Yes. Read rows using cin and use it as the loop bound. The last printed line will then contain just rows.
O(n²) for n rows: you print n+(n-1)+…+1 = n(n+1)/2 numbers.

Explore More C++ Number Patterns!

Continue practicing nested loops with more number patterns.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful