Reverse Growing Number Pattern in C++

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

What You’ll Learn

How to print the reverse growing number pattern 5, 54, 543, 5432, 54321 in C++ using nested for loops.

This pattern grows by one digit each row while staying in descending order within each row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5
54
543
5432
54321
1

Complete C++ Program

Fixed five rows: inner loop prints rows..i; as i decreases, the row grows.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    int i, j;

    for (i = rows; i >= 1; i--) {
        for (j = rows; j >= i; j--) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Choose the maximum number

int rows = 5; sets the maximum digit (5) and how many lines you print.

Setup
2

Outer loop (grow the row)

for (i = rows; i >= 1; i--) decreases i. A smaller i means the inner loop runs longer, so the row gains one extra digit each time.

Row control
3

Inner loop (print rows..i)

for (j = rows; j >= i; j--) prints descending digits starting from rows down to i. That yields 5, then 54, then 543, etc.

Number printing
4

New line

cout << "\n"; completes each row.

Line break
=

Reverse growing pattern

Total digits 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 = rows; j >= i; 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 << ' '
  • Print the non-reverse growing version (1, 12, ...) by counting up in the inner loop
  • Try printing letters instead of numbers to get an alphabet version
  • 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
  • Mixing row/column logic (keep the outer loop for rows)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop decreases the inner-loop lower bound, which makes each row longer.

2

The inner loop prints descending numbers from rows to i.

3

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

4

Small loop-bound tweaks are enough to switch between shrinking and growing patterns.

❓ Frequently Asked Questions

Because the outer loop decreases i. When i is smaller, the condition j >= i stays true for more values of j, so the inner loop prints more digits.
Yes. Use cout << j << ' '; inside the inner loop.
They use a similar inner loop (rows..i). The difference is the outer loop direction: Program 4 increases i (shrinking), while Program 8 decreases i (growing).
O(n²) for n rows: you print 1+2+…+n = n(n+1)/2 digits.

Explore More C++ Number Patterns!

Small changes in loop bounds can generate many pattern styles.

All Number Patterns →
Did you know?

You can think of this pattern as building a string by prepending one digit each row—but loops let you generate it without any string operations.

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