C++ Descending Number Triangle Pattern

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

What Is This Pattern?

A descending number triangle prints digits 1..i on each row, with the outer loop counting down from rows so the first line is longest.

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

12345
1234
123
12
1         ← rows = 5

First pattern in the C++ number series; next is the left-shifted triangle in Program 2.

How to Solve It

Outer loop sets the row length i (counting down). Inner loop prints 1..i with cout.

MethodIdeaBest for
Count-down outer loopOuter i = rows..1; inner j = 1..i; print jLearning, interviews, exams
Ascending variantOuter i = 1..rows with the same inner loopGrowing triangle (1, 12, 123…)

Pseudocode

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

Cheat sheet

GoalPattern
Pick each rowfor (i = rows; i >= 1; i--)
Print 1..ifor (j = 1; j <= i; j++) cout << j;
End of rowcout << "\n";
Spaced formcout << j << " ";
Ascending insteadOuter for (i = 1; i <= rows; i++)

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 descending 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
12345
1234
123
12
1

Worked Walkthrough — rows = 5

Trace how i shrinks and the inner loop always restarts at 1.

iInner jPrints
51..512345
41..41234
31..3123
11..11

Total digits = n(n+1)/2 → O(n²). Keep cout << "\n" outside the inner loop.

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 loop counts down; inner loop prints 1..i.

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 << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer sets the length. i starts at 5 and decreases to 1.

2. Inner prints digits. j always runs from 1 to i on that row.

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 << j;
        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 1..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 the count-down outer loop.

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 << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 3 → 123; i = 2 → 12; i = 1 → 1.

2. Trace on paper. If the outer loop counted up instead, you would get 1, 12, 123 — the ascending variant.

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

Ascending by mistake

If the outer loop counts up, you get 1, 12, 123… Keep i = rows..1 for this pattern.

\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.

j = i..rows

Got Program 2 instead

Starting the inner loop at i produces 12345, 2345, 345… — that is Program 2.

rows = 1

Single digit

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

rows > 9

Multi-digit width

Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced cout << j << " ".

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 = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Count down: outer i = rows..1 makes the first row the longest.
  • Always start at 1: inner j = 1..i rebuilds each row from the left.
  • Break the row: cout << j 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 1..i, then newline.

Frequently Asked Questions

A descending number triangle: the first row prints 1..rows, the next prints one fewer digit, down to a single 1 — for example 12345, 1234, 123, 12, 1.
Counting down makes the first row the longest. for (i = rows; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
Digits 1 through i. for (j = 1; j <= i; j++) cout << j; builds each row from left to right.
Change the outer loop to for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++) cout << j.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Program 1 always starts each row at 1. Program 2 starts at i and prints through rows (12345, 2345, 345…).
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are 1+2+…+n = n(n+1)/2.

Did you know?

Row i prints digits 1 through i. The outer loop counts down from rows, so the first line is longest and each row shortens by one digit — still O(n²) total prints.

Next: Left-Shifted Triangle

Continue with rows that start at i and run through rows.

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