C Decreasing Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned decreasing triangle prints rows down to i on each row. An indent loop adds two spaces per step, and %2d keeps columns aligned.

Remember
Rule: for i from rows down to 1
      print 2 spaces for j = 1..i-1
      print j with %2d for j = rows..i

         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1     ← rows = 5

Two inner loops — indent then numbers. Follows the incremental triangle in Program 35; next is Program 37.

How to Solve It

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

MethodIdeaBest for
Indent + %2dSpaces for j < i; then print rows..iLearning, interviews, exams
User input rowsSame loops; read rows with scanfReusable demos and labs

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i-1:
        print 2 spaces
    for j from rows down to i:
        print j (width 2)
    print newline

Cheat sheet

GoalPattern
Shrink outer boundfor (i = rows; i >= 1; i--)
Indent the rowfor (j = 1; j < i; j++) printf(" ");
Print rows..ifor (j = rows; j >= i; j--) printf("%2d", j);
End the rowprintf("\n");
Continuous counter insteadSee Program 35 — k++ with %3d

Printing Numbers vs Starting a New Line

APIEffectUse for
printf(" ") / printf("%2d", j)Stays on the same lineIndent spaces and each number
printf("\n")Ends the current lineAfter both inner loops

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

Live Preview

Change the row count and the right-aligned decreasing triangle updates instantly — each number uses a width-2 field like %2d.

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

Live result 5 rows · 15 numbers
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — rows = 4

Trace each outer-loop value of i from 4 down to 1, listing indent steps and numbers printed.

iIndent stepsNumbers (rows..i)Printed row
4344
324, 34 3
214, 3, 24 3 2
104, 3, 2, 14 3 2 1

Numbers printed for n rows = n(n + 1)/2. Indent work is also triangular — why time is O(n²).

C Programs

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

Example 1 — Fixed rows = 5

Hard-coded row count — indent loop then %2d countdown from rows.

C
#include <stdio.h>

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

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            printf("  ");

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

        printf("\n");
    }

    return 0;
}

How It Works

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

2. Indent loop. j runs from 1 to i - 1, printing two spaces each time.

3. Number loop. j runs from rows down to i with %2d.

When i = 5 you get a padded 5; when i = 1 you get 5 4 3 2 1 with no indent.

Example 2 — User Input Version

Read rows with scanf and reject invalid input. Same dual inner-loop core.

C
#include <stdio.h>

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

    printf("Enter rows: ");
    if (scanf("%d", &rows) != 1 || rows < 1)
        return 0;

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            printf("  ");

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Exit early if scanf fails or rows < 1.

2. Same core. Only the hard-coded 5 becomes rows in every loop bound.

3. Safer input tip. Prefer an explicit message instead of a silent exit:

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

Example 3 — Compact rows = 3

Same indent and number loops with a smaller row count for quick paper tracing.

C
#include <stdio.h>

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

    for (i = rows; i >= 1; i--)
    {
        for (j = 1; j < i; j++)
            printf("  ");

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — indent and %2d stay identical.

2. Trace on paper. For i = 2: one indent pair, then 3 and 2 → 3 2.

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

Upside-down triangle

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

1 space

Broken column alignment

A single space instead of " " does not match %2d width. Always print two spaces per indent step.

no %2d

Multi-digit drift

Plain printf("%d", j) crowds columns when values reach 10+. Keep %2d.

\n inside

Broken rows

If printf("\n") sits inside either inner loop, each cell lands on its own line. Call the newline only after both loops finish.

rows = 1

Smallest triangle

Output is a single padded 1 with no indent — 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)

Numbers printed = n(n + 1)/2; indent steps form another triangular sum. Extra memory stays constant aside from loop counters.

Key Takeaways

  • Rule: indent with " " for j < i, then print rows..i with %2d.
  • Start at rows: i = rows makes the first line a single padded digit.
  • Match widths: two spaces for indents and %2d for numbers so columns stay aligned.
  • Complexity: O(n²) time; O(1) extra space.

One line: for i from rows down to 1, indent i - 1 times, print rows..i with %2d, then printf("\n").

Frequently Asked Questions

A right-aligned decreasing triangle: the first row prints 5, then 5 4, then 5 4 3, down to 5 4 3 2 1.
An indentation loop prints two spaces while j < i before the number loop runs, pushing shorter rows to the right.
The format specifier reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for (j = rows; j >= i; j--), so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
Replace 5 with rows in the outer and number-loop bounds — see Example 2.
O(n²) for n rows because total indent steps plus number prints grow quadratically; numbers alone sum to n(n+1)/2.
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.

Did you know?

Each row starts from rows and counts down to i. An indentation loop prints two spaces per step (j = 1..i-1), then %2d keeps number columns aligned.

Next: Palindrome Number Triangle

Continue with a triangle that mirrors descending and ascending digits on each row.

Program 37 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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