C Number Triangle Pattern (Reverse Descending)

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

What Is This Pattern?

A reverse descending triangle prints each row from i down to 1. The outer loop starts at rows and shrinks, so the first line is longest.

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

54321
4321
321
21
1     ← rows = 5

Two nested descending loops. Follows the left-shifted triangle in Program 2; next is Program 4.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally space the digits for easier reading.

MethodIdeaBest for
Double countdownOuter i = rows..1; inner j = i..1Learning, interviews, exams
Spaced digitsSame loops; printf("%d ", j)Easier reading on long rows

Pseudocode

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

Cheat sheet

GoalPattern
Shrink rows downwardfor (i = rows; i >= 1; i--)
Print i..1for (j = i; j >= 1; j--) printf("%d", j);
End the rowprintf("\n");
Spaced variantprintf("%d ", j);
Ascending digits insteadfor (j = 1; j <= i; j++) printf("%d", j);

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j)Stays on the same lineEach digit in the row
printf("\n")Ends the current lineAfter the inner loop

Print all 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 so every value stays a single character.

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

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

Worked Walkthrough — rows = 4

Trace each outer-loop value of i from 4 down to 1 and list the digits printed.

iInner j = i..1Printed row
44, 3, 2, 14321
33, 2, 1321
22, 121
111

Total digits for n rows = n(n + 1)/2 (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a spaced-digits variant. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — both loops count downward.

C
#include <stdio.h>

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

    for (i = rows; i >= 1; i--)
    {
        for (j = i; j >= 1; j--)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop shrinks i. i runs from 5 down to 1 — first row is longest.

2. Inner loop prints i..1. When i = 5, j prints 5, 4, 3, 2, 1.

3. Newline. printf("\n") ends the row after the inner loop.

When i = 5 you get 54321; when i = 1 you get a single 1.

Example 2 — User Input Version

Read rows with scanf and clamp to 1–9. Same double-countdown core.

C
#include <stdio.h>

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

    printf("Enter rows (1-9): ");
    scanf("%d", &rows);
    if (rows < 1) rows = 1;
    if (rows > 9) rows = 9;

    for (i = rows; i >= 1; i--)
    {
        for (j = i; j >= 1; j--)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and clamp. Read rows, then force it into 1..9 so single-digit columns stay clear.

2. Same core. Only the hard-coded 5 becomes the user value.

3. Safer input tip. Unchecked scanf leaves rows uninitialized on bad input. Prefer:

Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Spaced digits

Keep rows = 5; print each digit followed by a space.

C
#include <stdio.h>

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

    for (i = rows; i >= 1; i--)
    {
        for (j = i; j >= 1; j--)
            printf("%d ", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Outer and inner countdown bounds are unchanged.

2. Add trailing spaces. Only the print statement changes to printf("%d ", j).

3. Learn the compact form first. Use Examples 1–2 for the classic no-space triangle; treat spacing as a polish option.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Upside-down triangle

If the outer loop counts up instead of down, the shortest row prints first. Use for (i = rows; i >= 1; i--).

j = 1..i

Wrong digit order

An ascending inner loop prints 12345 instead of 54321. Keep j counting down from i.

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each digit lands on its own line. Call the newline only after the row finishes.

rows > 9

Multi-digit values

printf("%d", 10) prints two characters and columns drift. Clamp demos to 1–9.

rows = 1

Smallest triangle

Output is a single 1 — a good sanity check.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(rows²)O(1)
Spaced digits (Example 3)O(rows²)O(1)

Total digits = n(n + 1)/2 — quadratic in n. For rows = 5 that is 15 printed digits.

Key Takeaways

  • Rule: for each i from rows down to 1, print i..1.
  • Start at rows: i = rows makes the first line the longest.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n(n + 1)/2 digits; O(1) extra space.

One line: for i from rows down to 1, print j from i down to 1, then printf("\n").

Frequently Asked Questions

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.
Keep the descending outer loop but change the inner loop to j = 1..i (ascending) — see Program 1.
Replace 5 with rows in the outer bound — see Example 2.
Use printf("%d ", j) instead of printf("%d", j) — see Example 3.
O(n²) for n rows. Total prints are n + (n-1) + ... + 1 = n(n+1)/2.
Check scanf's return value and reject values outside 1..9 so single-digit columns stay clear.

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 rows that always start at the max digit and count down to i.

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