Reverse Descending Number Triangle in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Descending Loops

What You’ll Learn

The reverse descending number triangle prints 54321, 4321, 321, 21, 1 — a natural step after the left-shifted triangle in Program 2. This tutorial covers descending outer and inner loops, a live preview, algorithm steps, worked C examples, edge cases, and complexity.

Shape Rule

i..1 per row

Row 1 prints 54321, row 2 prints 4321, shrinking until a single 1.

Outer Loop

rows..1

for (i = rows; i >= 1; i--) shrinks the row length each iteration.

Inner Loop

i..1 descending

for (j = i; j >= 1; j--) prints digits in reverse order on each row.

printf vs newline

Same line / next line

Digits use printf("%d", j); end each row with printf("\n").

Live Preview

3–9 rows

Pick a row count and draw the reverse descending triangle in the browser.

O(n²)

Complexity

Total digit prints = n(n+1)/2; extra memory stays O(1).

Introduction

A reverse descending number triangle prints each row from i down to 1 while the outer loop shrinks the row length. With rows = 5, the output is 54321, 4321, 321, 21, 1.

In C the outer loop runs i = rows..1, the inner loop prints j from i down to 1, then printf("\n") moves to the next line.

Why it matters?

It teaches descending inner loops — a key step after Program 2’s left-shifted ascending rows.

Key Highlights

Shrinking rows

Outer loop i = rows..1 shortens each row.

Reverse print

Inner loop j = i..1 counts downward.

vs Program 2

Program 2 ascends i..rows; Program 3 descends i..1.

Series Foundation

Follow Program 2; continue to Program 4 (left-aligned descending) next.

In short: for each i from rows down to 1, print j from i down to 1, then printf("\n").

📝 Problem & Approach

Given a positive integer rows (e.g. 5), print a reverse descending triangle: each row i shows digits from i down to 1, with the outer loop counting from rows down to 1.

c
// rows = 5 (conceptual shape)
// 54321
// 4321
// 321
// 21
// 1

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines — outer loop runs from rows down to 1.
iintOuter loop — current row limit; also the first digit printed.
jintInner loop — descending from i down to 1.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Nested loops54321, 4321, …Learning and interviews
User-input rowsscanf("%d", &rows);Flexible console programs
Spaced outputprintf("%d ", j)Easier reading per row

⚡ Quick Reference

GoalPattern
Walk rowsfor (i = rows; i >= 1; i--)
Print digits i..1for (j = i; j >= 1; j--) printf("%d", j);
End the rowprintf("\n");
Spaced digitsprintf("%d ", j);
User inputscanf("%d", &rows);
Program 2 contrastfor (i = 1; i <= rows; i++) with j = i..rows

📋 Fixed Rows vs User Input vs Spaced Output

Same reverse descending triangle — different ways to control rows and formatting.

Outer loop
i = rows..1

Shrinks row length each line

Inner loop
j = i..1

Descending digits per row

First row
i = rows

Longest row on top

Learning tip
j--

Inner loop must count down

Context

When This Pattern Shows Up

Reach for this pattern when teaching descending inner loops and shrinking row lengths.

  1. Post left-shift exercise

    Natural follow-up after Program 2 — introduces a descending inner loop.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with scanf for a flexible row count.

  4. Gateway to variants

    Compare Program 2 (left-shifted) and Program 4 (left-aligned descending) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a row count between 3 and 9 and draw the reverse descending triangle in the browser.

Try 4, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of the reverse descending triangle with nested descending loops.

Example 1 — Fixed rows = 5

Hard-coded row count — ideal for first demos and screenshots.

c
#include <stdio.h>

int main() {
    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

When i = 5, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When i = 1, only one digit prints — output 1. The outer loop shrinks i each row.

📈 User Input

Read the row count with scanf instead of hard-coding 5.

Example 2 — User Input

Read rows with scanf("%d", &rows); both loops use rows as the starting bound.

c
#include <stdio.h>

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

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

        printf("\n");
    }

    return 0;
}

How It Works

Same descending-loop core as Example 1; only the source of rows changes from a literal to user input. Non-numeric input leaves rows unset if you ignore scanf’s return value — always check it in safer labs.

⚡ Spaced Output

Add a space between digits for easier reading on each row.

Example 3 — Spaced Digits

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

c
#include <stdio.h>

int main() {
    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

Only the print statement changes — printf("%d ", j) instead of printf("%d", j). Loop bounds stay the same as Example 1.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Set loop variables i, j with rows = 5.

Setup
2

Outer loop walks rows

for (i = rows; i >= 1; i--) — descending outer loop shrinks each row.

Row
3

Inner loop (j)

for (j = i; j >= 1; j--) — prints digits i..1 in reverse order.

Reverse
4

New line

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

Break
=

Reverse descending triangle complete

Rows shrink from rows digits to one — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value of i, the inner-loop range, digit count, and full row output.

iInner loop (j)PrintsRow output
55, 4, 3, 2, 1554321
44, 3, 2, 144321
33, 2, 13321
22, 1221
1111

Prints per row = i — total prints = n(n+1)/2 for n rows.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: flip j-- to j++ and watch digit order change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: continue to Program 4 for a left-aligned descending triangle.

3. Console Formatting Drills

Practice Write vs printf("\n") without complex math.

Example: put printf("\n") inside the inner loop by mistake.

4. Spaced Output

Add spaces between digits once the two-loop structure works.

Example: use printf("%d ", j) between digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).

6. Input Validation Labs

Pair the pattern with scanf return checks and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner C courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row shortens by one digit.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Match Descending Bounds

    Outer loop counts down (i--); inner loop must also count down from i to 1.

  2. 2. Prefer scanf

    Check the return value so bad input does not leave rows uninitialized.

  3. 3. Keep printf("\n") Outside

    Only call printf("\n") after the inner loop finishes the row.

  4. 4. Count Down in the Inner Loop

    for (j = i; j >= 1; j--) prints digits i..1 in reverse order.

  5. 5. Dry-Run rows = 3

    Trace i = 3, 2, 1 on paper before coding the full rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put printf("\n") inside the inner loop.

Common Pitfalls

Mistakes that commonly break reverse descending number triangles.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use printf("%d", j) for digits; printf("\n") only after the inner loop.

  2. 2. Wrong Inner Direction

    for (j = 1; j <= i; j++) prints ascending digits — you get Program 1’s shape, not this one.

    → Keep for (j = i; j >= 1; j--) so each row reads i..1.

  3. 3. Ascending Outer Loop

    for (i = 1; i <= rows; i++) grows rows instead of shrinking them.

    → Use for (i = rows; i >= 1; i--) so the first row is the longest.

  4. 4. Wrong Inner Start

    j = rows on every row prints the same full line repeatedly.

    → Start the inner loop at the current outer value: j = i.

  5. 5. Unchecked scanf

    Letters or empty input leave rows uninitialized.

    → Check scanf return value and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single digit row

Output is just 1 on one line.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 21 and 1.

Bad input

Non-numeric scanf input

Unchecked scanf leaves rows unset — check the return value.

Large rows

Large row count

Each row prints i digits — total work grows as n(n+1)/2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Classic descending triangle

  • Outer loop counts up; inner prints 1..i
  • Review Program 1

2. Left-shifted triangle

  • Compare with Program 2
  • Same outer loop, different inner bounds

3. Left-aligned descending

  • Continue with Program 4
  • Inner loop starts at rows each row

4. Spaced output

  • Use printf("%d ", j) between digits
  • Same loops, wider visual spacing

Notes

  • Descending rule. Outer loop: i = rows..1. Inner loop: j = i..1 with j--.
  • printf stays on the line; printf("\n") advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single 1.
  • Row i prints exactly i digits — compare with Program 2 where each row prints rows - i + 1 digits.

Quick Takeaway: outer loop i = rows..1, inner loop j = i..1 with printf("%d", j), then printf("\n").

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The reverse descending number triangle is a compact nested-loop lesson: a descending outer loop shrinks each row while the inner loop prints digits from i down to 1. Master the fixed-rows version, then try user input and spaced output.

Practice the three examples above, then continue to Program 4 for the left-aligned descending number triangle.

Row i prints i..1 — keep printf("%d", j) for digits and printf("\n") for the break, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Use for (i = rows; i >= 1; i--) in the outer loop
  • Inner: for (j = i; j >= 1; j--) prints digits in reverse
  • Use printf("%d", j) for digits and printf("\n") after each row
  • Validate rows ≥ 1 for interactive programs
  • Check scanf return value before using rows

❌ Don’t

  • Call printf("\n") inside the inner digit loop
  • Use ascending inner loop when you meant reverse order
  • Use ascending outer loop when rows should shrink
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this reverse descending triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

Counts down rows

Code
03

Inner loop

j = i down to 1

Code
04

Newline

Ends each row

Shape
O 05

Complexity

O(n²) time

Analysis

❓ 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).
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: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
Only one row prints — a single 1.

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.

Continue to Program 4

Move on to the left-aligned descending number triangle in the C number-pattern series.

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