C++ Repeating Number Triangle Pattern (Inverted)

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

What Is This Pattern?

An inverted repeating number triangle prints digit i exactly i times while i counts down — widest row first: 55555, 4444, 333, 22, 1.

Remember
Rule: for i from rows down to 1
        for j from 1 to i: print i (no space)

55555
4444
333
22
1         ← rows = 5

Follows the descending repeating triangle in Program 10; next is the shrinking repeating pattern in Program 12.

How to Solve It

Outer loop picks digit i (counting down). Inner loop runs i times and prints i each time — same inner bound as Program 9, flipped outer direction.

MethodIdeaBest for
Count-down outer + print iOuter i = rows..1; inner j = 1..i; print iLearning, interviews, exams
String shortcutcout << string(i, char('0' + i))Shorter code once the bound is clear

Pseudocode

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

Cheat sheet

GoalPattern
Pick each digitfor (i = rows; i >= 1; i--)
Repeat digitfor (j = 1; j <= i; j++) cout << i;
End of rowcout << "\n";
vs Program 9Same inner bound; 9 ascends, 11 descends
vs Program 1010 uses j = rows..i (growing repeats); 11 uses j = 1..i

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 inverted repeating triangle 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
55555
4444
333
22
1

Worked Walkthrough — rows = 5

Trace how the digit and the repeat count both shrink together.

iInner runsPrints
55 times55555
44 times4444
33 times333
11 time1

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 counts down; inner prints 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. Outer picks the digit. i starts at 5 and decreases to 1.

2. Inner matches the count. j runs from 1 to i — that is exactly i 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 = rows; i >= 1; i--)
    {
        for (j = 1; j <= i; 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 count-down / inner print-i 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 widest-first repeating digits.

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

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

2. Trace on paper. If the outer loop counted up instead, you would 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.

i = 1..rows

Got Program 9 instead

An ascending outer loop yields 1, 22, 333… Keep outer rows..1 for the inverted shape.

j = rows..i

Got Program 10 instead

Using for (j = rows; j >= i; j--) grows repeats as the digit falls (5, 44, 333…). Keep j = 1..i 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

  • Widest first: outer i = rows..1 starts with the longest row.
  • Print i, i times: inner j = 1..i — same bound as Program 9, opposite outer direction.
  • 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 rows down to 1, print i exactly i times, then newline.

Frequently Asked Questions

An inverted repeating triangle: for rows=5 you get 55555, 4444, 333, 22, 1 — widest row first, then each line shortens.
The outer loop decreases i from rows to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times.
for (j = 1; j <= i; j++) cout << i runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
Program 10 grows repeats as the digit shrinks (5, 44, 333…). Program 11 repeats digit i exactly i times (55555, 4444, 333…).
Program 9 uses an ascending outer loop (1, 22, 333…). Program 11 uses the same inner bound but counts the outer loop down (55555, 4444, 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 exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.

Next: Shrinking Repeating Pattern

Continue with ascending digits that shrink each row (11111, 2222, 333…).

Program 12 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