C++ Repeating Number Pattern (Shrinking)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A shrinking repeating number pattern prints digit i with a falling repeat count as i counts up — 11111, 2222, 333, 44, 5.

Remember
Rule: for i from 1 to rows
        repeat print i exactly (rows - i + 1) times

11111
2222
333
44
5         ← rows = 5

Follows the inverted repeating triangle in Program 11; next is the alternating zigzag triangle in Program 13.

How to Solve It

Outer loop picks digit i (counting up). Inner loop runs from i to rows and prints i each time — that is rows - i + 1 repeats.

MethodIdeaBest for
Ascending outer + i..rows innerOuter i = 1..rows; inner j = i..rows; print iLearning, interviews, exams
Counted repeatsInner j = 1..(rows - i + 1); print iSame shape, clearer shrink formula

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print i
    print newline

Cheat sheet

GoalPattern
Pick each digitfor (i = 1; i <= rows; i++)
Repeat digitfor (j = i; j <= rows; j++) cout << i;
End of rowcout << "\n";
Repeat countrows - i + 1 (shrinks as i grows)
vs Program 99: j = 1..i (grows); 12: j = i..rows (shrinks)

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << iStays on the same lineEach repeated digit on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the shrinking repeating pattern updates instantly — capped at 9 for readable single-digit demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 digits
11111
2222
333
44
5

Worked Walkthrough — rows = 5

Trace how the digit rises while the repeat count falls.

iInner runsPrints
15 times11111
24 times2222
33 times333
51 time5

Total digits = n(n+1)/2 → O(n²). Always print i, never j.

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer picks digit i; inner runs from i to rows.

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. Outer picks the digit. i starts at 1 and increases to rows.

2. Inner sets repeats. j runs from i to rows — that is rows - i + 1 prints of i.

3. Newline. Call cout << "\n" only after the inner loop finishes the row.

Example 2 — User Input Rows

Read rows with cin, validate, then run the same nested loops.

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

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

    cout << "Enter the number of rows: ";
    if (!(cin >> rows) || rows <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

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

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Outer ascending / inner i..rows matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm digit vs shrink count.

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

int main()
{
    int rows = 3;
    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. Three rows. i = 1 → 111; i = 2 → 22; i = 3 → 3.

2. Trace on paper. If you use j = 1..i instead, you get Program 9 (1, 22, 333).

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

j = 1..i

Got Program 9 instead

Using for (j = 1; j <= i; j++) grows the row (1, 22, 333…). Keep j = i..rows to shrink.

i = rows..1

Got Program 11 instead

A descending outer loop with j = 1..i yields 55555, 4444… Keep outer 1..rows here.

print j

Wrong values on the row

If you print j instead of i, you get changing digits per position. Always print the outer variable.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

rows = 1

Single digit

Output is just 1 — a good sanity check for input validation.

cin

Check the stream

Validate cin >> rows before looping — a failed read leaves rows unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Total digits printed = n + (n-1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Count up: outer i = 1..rows picks a rising digit.
  • Shrinking repeats: inner j = i..rows runs rows - i + 1 times and always prints i.
  • Break the row: cout << i in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: for each i from 1 to rows, print i exactly rows - i + 1 times, then newline.

Frequently Asked Questions

A shrinking repeating pattern: for rows=5 you get 11111, 2222, 333, 44, 5 — digit rises each row while the repeat count falls.
The inner loop runs from j = i to rows. As i increases, the inner loop runs fewer times, so each row prints fewer copies of the row digit.
On the first row, i = 1 and the inner loop runs from 1 to rows, so it prints 1 exactly rows times.
Program 11 counts down and repeats digit i exactly i times (55555, 4444…). Program 12 counts up and uses for (j = i; j <= rows; j++) so digit i repeats rows - i + 1 times.
Program 9 grows: row i repeats i times (1, 22, 333). Program 12 shrinks: row i repeats rows - i + 1 times (11111, 2222, 333).
cout << i stays on the same line. cout << "\n" ends the current line. Repeated digits use cout << i; the row break uses cout << "\n" after the inner loop.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are n+(n-1)+…+1 = n(n+1)/2.

Did you know?

Row digit i repeats rows − i + 1 times. The outer loop counts up from 1, while the inner loop runs from i to rows, so each row shrinks — still O(n²) total prints.

Next: Alternating Zigzag Triangle

Continue with rows that flip between ascending and descending digit runs.

Program 13 tutorial →

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