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 repeated number triangle pattern 1, 22, 333, 4444, 55555 in C++ using nested for loops.

The trick is printing the row index i inside the inner loop, so each row repeats the same digit.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
22
333
4444
55555
1

Complete C++ Program

Fixed five rows: each row prints the same number (i) exactly i times.

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

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

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

    return 0;
}

🧠 How It Works

1

Choose the row count

int rows = 5; sets how many lines will be printed.

Setup
2

Outer loop (row value)

for (i = 1; i <= rows; i++) chooses which digit is repeated on that row. When i is 4, the row is made of 4s.

Row control
3

Inner loop (repeat i)

for (j = 1; j <= i; j++) runs i times. Printing cout << i repeats the same digit across the row.

Repeat
4

New line

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

Line break
=

Repeated number 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 = 1; i <= rows; i++) {
        for (j = 1; 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 << ' '
  • Invert the pattern by running the outer loop from rows down to 1
  • Print a different symbol per row (letters, stars) using the same loop structure
  • 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
  • Printing j instead of i (that becomes the ascending triangle pattern)
  • Flushing output with endl unnecessarily in tight loops

Key Takeaways

1

The outer loop chooses the digit to repeat on that row.

2

The inner loop repeats the digit i exactly i times.

3

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

4

The only change from an ascending triangle is printing i instead of j.

❓ Frequently Asked Questions

Printing i repeats the row number on the same line. If you print j, you get 1, 12, 123, ... which is a different pattern.
Use cout << i << ' '; inside the inner loop.
Reverse the outer loop: for (i = rows; i >= 1; i--) and keep printing 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!

Once you master this, try mixing repeated digits with spacing and alignment.

All Number Patterns →
Did you know?

This pattern is a classic way to practice nested loops: the inner loop length depends on the outer loop index. It’s the same structure behind many triangles and pyramids.

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