C++ Reverse Number Triangle Pattern (Growing)

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

What Is This Pattern?

A reverse row number triangle grows one digit per row, printing each row from the row index down to 1 — so you see 1, 21, 321, and so on.

Remember
Rule: outer i = 1..rows; inner j = i..1

1
21
321
4321
54321     ← rows = 5

Unlike Program 5 (1..i ascending) or Program 6 (i..rows left-growing), this pattern counts the outer loop up and prints each row descending.

How to Solve It

Outer loop from 1 to rows. Inner loop from current i down to 1. Print each digit, then end the line.

MethodIdeaBest for
Nested loopsOuter up, inner i..1Learning, interviews, demos
Compact traceSame logic with rows = 3Quick dry-runs on paper

Pseudocode

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

Cheat sheet

GoalPattern
Outer (row grows)for (i = 1; i <= rows; i++)
Inner (countdown)for (j = i; j >= 1; j--)
Print digitcout << j;
End the rowcout << "\n";
Digits on row ii digits

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit
cout << "\n"Ends the lineAfter the inner loop

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

Live Preview

Change the row count and the reverse row triangle updates instantly.

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

Live result 5 rows · 15 digits
1
21
321
4321
54321

Worked Walkthrough

Trace three outer values when rows = 5 — watch each row count down to 1.

Outer iInner jPrints
11..11
33..1321
55..154321

Each new row adds one digit on the left while still ending at 1.

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 up from 1, inner down from i to 1.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j >= 1; j--)
            cout << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer grows the row. i runs from 1 to rows — that is both the row number and the first digit.

2. Inner counts down. j runs from i to 1, printing the reverse sequence for that row.

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

Example 2 — User Input Rows

Read the row count at runtime with a simple validation tip.

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

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j >= 1; j--)
            cout << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Same loop core. Only the source of rows changes from a literal to cin.

2. Entering 4 stops early. You get four rows ending at 4321.

3. Validate in real apps. Prefer checking cin failure and requiring a positive height (tip below).

Safer input tip
if (!(cin >> rows) || rows < 1)
{
    cout << "Please enter a positive integer.\n";
    return 1;
}

Example 3 — Compact rows = 3

Smaller height for a quick paper trace of both loop bounds.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j >= 1; j--)
            cout << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Only three rows. Easy to dry-run every i and j value on paper.

2. Same formula. Nothing changes except rows — proving the pattern scales.

3. Countdown still holds. Row 2 prints 21 — start at 2, end at 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

Inner direction

Count down with j--

Using j++ from 1 to i reprints Program 5 (1, 12, 123) instead of this pattern.

Newlines

Do not put "\n" inside the inner loop

That prints one digit per line and destroys the triangle.

Start value

Start the inner loop at i, not at rows

Starting at rows every time prints the same full countdown on every line.

Bad cin

Validate input

Check cin >> rows and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Triangle (Examples 1–3)O(n²)O(1)

Digits printed are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Extra memory is only the loop variables.

Key Takeaways

  • Rule: outer i = 1..rows, inner j = i..1.
  • Growth: each row adds one digit on the left and still ends at 1.
  • Break the row: call cout << "\n" only after the inner loop finishes.
  • Complexity: O(n²) time, O(1) extra space.

One line: grow the row index, then count that index down to 1.

Frequently Asked Questions

A reverse row number triangle: row 1 shows 1, row 2 shows 21, row 3 shows 321, and so on until the last row shows digits from rows down to 1.
The inner loop starts at j = i and decrements to 1. That prints i, i-1, ..., 1 on each row.
When i = rows (5), the inner loop prints j from 5 down to 1 — giving 54321 on the last line.
Program 6 outer counts down and prints i..rows (5, 45, 345). Program 7 outer counts up and prints i down to 1 (1, 21, 321).
Program 5 prints 1..i ascending per row. Program 7 prints i..1 descending per row.
Printing a digit stays on the same line. Printing a newline ends the current row. Digits use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.

Did you know?

Each row prints digits in reverse order — outer i runs 1..rows, inner j counts down from i to 1 — producing 1, 21, 321, and so on. Total prints grow as O(n²).

Next: Reverse Left-Growing Triangle

Continue with the next pattern in the C++ number-pattern series.

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