Ascending Repeating Shrinking Pattern in C++

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

What You’ll Learn

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

The idea is simple: the row digit increases (1, 2, 3, ...), while the number of repeats decreases because the inner loop starts at j = i.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
44
5
1

Complete C++ Program

The outer loop increases from 1 to rows. The inner loop runs from i to rows and prints i, creating fewer repeats on each next line.

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

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

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

    return 0;
}

🧠 How It Works

1

Set the size

int rows = 5; sets the height of the pattern and the maximum digit.

Setup
2

Outer loop (digit per row)

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

Row control
3

Inner loop (shrink repeats)

for (j = i; j <= rows; j++) runs fewer times as i grows. Each iteration prints i, so the row becomes i repeated.

Number printing
4

New line

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

Line break
=

Shrinking repeats

Total digits printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Read rows from the user so the pattern size can be changed at runtime:

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 = i; j <= rows; 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 using cout << i << ' ';
  • Right-align the pattern by printing leading spaces before each row
  • Print the decreasing-digit version by running the outer loop from rows down to 1
  • Switch to characters to create alphabet patterns

Avoid

  • Forgetting to print a newline after each row
  • Hard-coding 5 throughout (use rows instead)
  • Using endl inside loops (it flushes output)
  • Not handling rows <= 0 if you accept user input

Key Takeaways

1

The outer loop chooses the digit per row (1, 2, 3, ...).

2

The inner loop controls the row length by starting at j = i.

3

This produces repeated digits with shrinking width: 11111 to 5.

4

Total prints still follow the triangular count: n(n+1)/2.

❓ Frequently Asked Questions

Because the inner loop starts at j = i. When i increases, the loop runs fewer times, so each line contains fewer digits.
Because the code prints i (not j) inside the inner loop. That makes each row a repetition of the same digit.
Print j inside the inner loop: cout << j;. That changes the output to a shifting sequence pattern.
O(n²) for n rows: total prints are n+(n-1)+…+1 = n(n+1)/2.

Explore More C++ Number Patterns!

Try more repeating and non-repeating patterns to master nested loops.

All Number Patterns →
Did you know?

Starting the inner loop from j = i is a common trick to create shrinking patterns without extra counters.

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