Reverse Repeated Number Triangle in C++

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

What You’ll Learn

How to print the reverse repeated number triangle pattern 5, 44, 333, 2222, 11111 in C++ using nested for loops.

Each row repeats the same digit, but the digit decreases while the count increases.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
5
44
333
2222
11111
1

Complete C++ Program

Fixed five rows: outer loop chooses the digit; inner loop repeats it rows-i+1 times.

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 << i;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Choose the row count

int rows = 5; defines how many lines to print.

Setup
2

Outer loop (digit)

for (i = rows; i >= 1; i--) chooses the digit printed on each row (5, then 4, then 3...).

Row value
3

Inner loop (repeat)

for (j = rows; j >= i; j--) controls how many times the digit repeats. When i is smaller, the loop runs more times, so the row gets longer.

Repeat
4

New line

cout << "\n"; moves to the next row.

Line break
=

Reverse repeated-digit triangle

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 number of rows at runtime 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 << i;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Add spaces between digits with cout << i << ' '
  • Switch to ascending repeated digits by looping i from 1 to rows (Program 9)
  • Try right-aligning by printing spaces before each row
  • Use characters instead of digits to make an alphabet variant

Avoid

  • Forgetting to print a newline between rows
  • Hard-coding 5 everywhere instead of using rows
  • Printing j instead of i (that prints a different pattern)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop selects the digit and counts down from rows to 1.

2

The inner loop controls the row length by running from rows down to i.

3

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

4

This is the reverse counterpart of Program 9 (ascending repeated-digit triangle).

❓ Frequently Asked Questions

When i is smaller, the condition j >= i stays true for more values of j, so the inner loop runs more times. That increases the number of repeats.
Program 9 uses i increasing from 1 to rows. Here, i decreases from rows to 1, producing the reverse order of repeated digits.
Yes. Use cout << i << ' '; inside the inner loop.
O(n²) for n rows: you print 1+2+…+n = n(n+1)/2 digits.

Explore More C++ Number Patterns!

Try combining this repeated-digit idea with alignment and spacing for more complex patterns.

All Number Patterns →
Did you know?

This pattern is a neat example of two moving pieces: the digit decreases each row, but the repeat count increases. It comes from changing the inner loop bound to depend on i.

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