C++ Number Triangle Pattern (Reverse Descending)

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

What Is This Pattern?

A reverse descending number triangle prints each row from i down to 1 while the outer loop shrinks from rows to 1.

Remember
Rule: for i from rows down to 1
        print j from i down to 1

54321
4321
321
21
1     ← rows = 5

Follows the left-shifted descending triangle in Program 2; next is the left-aligned descending triangle in Program 4.

How to Solve It

Outer loop shrinks i. Inner loop prints i..1 so each row reads in reverse and gets shorter.

MethodIdeaBest for
Two descending loopsOuter rows..1, inner i..1Learning, interviews, exams
Spaced digitsSame loops with cout << j << " "Easier reading for larger rows

Pseudocode

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

Cheat sheet

GoalPattern
Shrink each rowfor (i = rows; i >= 1; i--)
Print i..1for (j = i; j >= 1; j--) cout << j;
End of rowcout << "\n";
Spaced digitscout << j << " ";
Ascending insteadfor (j = 1; j <= i; j++) → Program 1 style

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach 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 reverse descending triangle updates instantly — capped at 9 for readable 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
54321
4321
321
21
1

Worked Walkthrough — rows = 5

Trace how each outer-loop value of i builds a shorter descending row.

iInner jDigitsPrints
55..1554321
44..144321
33..13321
22..1221
11..111

Total digits = 5 + 4 + 3 + 2 + 1 = 15 → n(n+1)/2 → O(n²).

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 shrinks rows..1, inner prints i..1.

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

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

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer shrinks the start. i begins at rows and decreases — the longest row prints first.

2. Inner counts down. Print j from i down to 1 so each row reads in reverse.

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

Example 2 — User Input Rows

Read rows with cin, validate, then use the same two-loop core.

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

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

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

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

        cout << "\n";
    }

    return 0;
}

How It Works

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

2. Same core. Descending outer + descending inner matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable 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 both loops count downward.

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

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

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 3 → 321; i = 2 → 21; i = 1 → 1.

2. Trace on paper. If the outer loop counts up instead of down, the shortest row prints first.

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++

Growing outer loop

Using for (i = 1; i <= rows; i++) prints the shortest row first. Keep i = rows..1.

j++

Ascending inner loop

for (j = 1; j <= i; j++) prints 12345 style rows — not reverse. Use j = i..1.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, you get one digit per line. Call it only after the inner loop.

j = rows

Full line every row

Starting j at rows every time reprints 54321 on each line. Start at i.

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)

Row starting at i prints i digits. Total = n + (n−1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Two descending loops: outer rows..1, inner i..1.
  • Longest first: start the outer loop at rows so the first row is the widest.
  • Break the row: digits in the inner loop; cout << "\n" after it finishes.
  • 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..1, then newline.

Frequently Asked Questions

A reverse descending triangle: for rows=5 you get 54321 / 4321 / 321 / 21 / 1 — each row prints i down to 1 while the outer loop shrinks.
The inner loop runs j = i down to 1, so each row prints i, i-1, ..., 1.
Because rows = 5 and the outer loop starts with i = rows. The first row prints from 5 down to 1.
Program 2 prints i..rows (left-shifted). Program 3 prints i..1 (reverse descending) with a shrinking outer loop.
Use cout << j << " " instead of cout << j in the inner loop.
cout << j prints digits on the same line. cout << "\n" ends the row after the inner loop finishes.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows. Total prints are n + (n-1) + … + 1 = n(n+1)/2.

Did you know?

This pattern prints each row in descending order from i down to 1. The outer loop shrinks the row length while the inner loop counts downward — producing 54321, 4321, 321, and so on.

Next: Left-Aligned Descending Triangle

Continue with a left-aligned pattern of descending digits.

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