Increasing Suffix Number Pattern in C++

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

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:

Output
5
45
345
2345
12345
1

Complete C++ Program

Fixed five rows: outer loop moves the starting number left; inner loop prints from i up to rows.

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

1

Choose the maximum number

int rows = 5; sets the maximum number and total rows.

Setup
2

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.

Row control
3

Inner loop (print i..rows)

for (j = i; j <= rows; j++) prints from the current start i up to rows using cout << j.

Number printing
4

New line

cout << "\n"; completes each row before moving on.

Line break
=

Increasing suffix pattern

Total numbers printed: 1+2+…+n = 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 = 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 using rows
  • 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 rows instead of j
  • Right-align the triangle by printing leading spaces before each row

Avoid

  • Forgetting to print a newline between rows
  • Hard-coding 5 everywhere instead of using rows
  • Starting the inner loop at 1 (that would create a different triangle)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop moves the start value from rows down to 1.

2

The inner loop prints numbers from i up to rows on each line.

3

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

4

This is the mirror idea of Program 2: one removes the left side, the other builds it.

❓ Frequently Asked Questions

On the first iteration, i equals rows, so the inner loop prints only rows (just 5). Each next row starts one value earlier, so the line grows.
Program 2 prints 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).
Yes. Print cout << j << ' '; inside the inner loop.
O(n²) for n rows: you print 1+2+…+n = n(n+1)/2 numbers.

Explore More C++ Number Patterns!

Keep going to learn more number patterns and loop tricks.

All Number Patterns →
Did you know?

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.

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