C Descending Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned descending triangle prints leading spaces, then digits from i down to 1. A fixed-width inner loop keeps every row the same length so digits line up on the right.

Remember
Rule: for i from 1 to rows
      for j from rows down to 1:
        space if j > i, else digit j

    1
   21
  321
 4321
54321     ← rows = 5

One fixed-width countdown with a space-or-digit choice. Follows the spaced mirror in Program 29; next is Program 31.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.

MethodIdeaBest for
Fixed width + spacesCountdown j = rows..1; space if j > iLearning, interviews, exams
Ternary printSame loop; (j > i) ? printf(" ") : printf("%d", j)Compact code after you know if/else

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to 1:
        if j > i: print space
        else: print j
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Fixed column widthfor (j = rows; j >= 1; j--)
Space or digitif (j > i) printf(" "); else printf("%d", j);
End the rowprintf("\n");
Ternary form(j > i) ? printf(" ") : printf("%d", j);

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf(" ")Stays on the same lineDigits and leading spaces
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the row count and the right-aligned 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 · 25 cells
    1
   21
  321
 4321
54321

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and list spaces vs digits (spaces shown as · below for clarity).

iInner j = 4..1Printed row
1···11
2··2121
3·321321
443214321

Each of n rows prints n characters → n² cells total. That square count is why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input with ternary prints, and a small demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — if/else space-or-digit in a fixed-width countdown.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 5; j >= 1; j--)
        {
            if (j > i)
                printf(" ");
            else
                printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop grows i. i runs from 1 to 5 — one right-aligned row per pass.

2. Fixed-width countdown. j always runs 5..1; print a space when j > i, otherwise digit j.

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

When i = 1 you get 1; when i = 5 you get 54321 with no leading spaces.

Example 2 — User Input Version

Read rows with scanf and clamp to 1–9. Ternary operators compact the space-or-digit choice.

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 = 1; i <= rows; i++)
    {
        for (j = rows; j >= 1; j--)
            (j > i) ? printf(" ") : 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 aligned.

2. Same core. Only the hard-coded 5 becomes rows; ternaries replace if/else.

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 — Compact rows = 3

Same if/else logic with a smaller row count for quick paper tracing.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        for (j = rows; j >= 1; j--)
        {
            if (j > i)
                printf(" ");
            else
                printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — the fixed-width countdown stays identical.

2. Trace on paper. For i = 2: print space, then 2, then 1 → 21.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for the classic five-row triangle.

Edge Cases & Pitfalls

Check these before calling the solution done.

no spaces

Left-aligned by mistake

If you skip the space branch, digits flush left like Program 3. Keep printing spaces while j > i.

short loop

Variable-width rows

Looping only to i (not rows) drops the padding spaces. The inner loop must always run the full width.

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each character 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 with no leading spaces — 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)
Small demo (Example 3)O(rows²)O(1)

Each of n rows prints n characters → n² cells total. Extra memory stays constant aside from loop counters.

Key Takeaways

  • Rule: countdown j = rows..1; space if j > i, else print j.
  • Fixed width: the inner loop always runs to rows so columns stay right-aligned.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n² cells; O(1) extra space.

One line: for each i, countdown j from rows to 1 printing space or j, then printf("\n").

Frequently Asked Questions

Leading spaces are printed while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs j from rows down to 1. When j <= i, it prints j — naturally producing i..1 on each row.
Running j from rows down to 1 on every row keeps column alignment. Spaces fill positions where j > i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
Replace 5 with rows in the inner loop bound — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
Check scanf's return value and reject values outside 1..9 so single-digit columns stay aligned.
Yes — (j > i) ? printf(" ") : printf("%d", j) compacts the if/else logic.

Did you know?

This pattern uses a fixed column width (rows). For each row i, the inner loop prints spaces while j > i, then prints digits in descending order — producing a right-aligned triangle ending in 54321.

Next: Number-Star Diamond Pattern

Continue the C number-pattern series with a diamond that alternates digits and stars.

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