Inverted Repeating Number Triangle in C++

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

What You’ll Learn

How to print an inverted repeating number triangle in C++. Each row repeats the same digit (the row number), and the number of digits decreases every line: 55555, 4444, 333, 22, 1.

This is a great nested-loop exercise: the outer loop controls the current digit, while the inner loop controls how many times that digit is printed.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
55555
4444
333
22
1
1

Complete C++ Program

The outer loop counts down from rows to 1. The inner loop repeats the current row value i exactly i times.

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

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

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

    return 0;
}

🧠 How It Works

1

Choose the size

int rows = 5; sets both the first digit and the height of the triangle.

Setup
2

Outer loop (count down)

for (i = rows; i >= 1; i--) picks the digit to print on each row: 5, 4, 3, 2, 1.

Row control
3

Inner loop (repeat i)

for (j = 1; j <= i; j++) prints the same number i exactly i times using cout << i.

Number printing
4

New line

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

Line break
=

Inverted repeating 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 choose the size of the triangle 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 = 1; j <= i; j++) {
            cout << i;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate user input (e.g., check cin.fail()) before using rows
  • Add spaces for readability: cout << i << ' ';
  • Right-align the triangle by printing leading spaces before each row
  • Replace digits with characters (A, B, C...) to build alphabet patterns
  • Print the non-repeating version by outputting j instead of i

Avoid

  • Forgetting to print a newline after each row
  • Using endl inside loops (it flushes and can slow output)
  • Mixing row and column logic (keep outer = digit/row, inner = repeats)
  • Allowing rows <= 0 without handling it

Key Takeaways

1

The outer loop counts down from rows to 1, setting the digit for each row.

2

The inner loop repeats that digit exactly i times.

3

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

4

Changing what you print (i vs j) creates many new patterns with the same loop skeleton.

❓ Frequently Asked Questions

Because the program prints i inside the inner loop. Since i stays constant for that row, you get repeated digits like 55555 and 4444.
Change the outer loop to count up: for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++).
Print a space after each digit: cout << i << ' ';. This improves readability, especially for larger patterns.
O(n²) for n rows: you print n+(n-1)+…+1 = n(n+1)/2 digits.

Explore More C++ Number Patterns!

Keep practicing nested loops with more patterns and variations.

All Number Patterns →
Did you know?

Even though the digits change each row, the total number of printed characters is still triangular: n(n+1)/2 for n rows.

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