C Descending Number Triangle Pattern (Right-Aligned)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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.
Approach
How to Solve It
Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.
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
Goal
Pattern
Walk each row
for (i = 1; i <= rows; i++)
Fixed column width
for (j = rows; j >= 1; j--)
Space or digit
if (j > i) printf(" "); else printf("%d", j);
End the row
printf("\n");
Ternary form
(j > i) ? printf(" ") : printf("%d", j);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j) / printf(" ")
Stays on the same line
Digits and leading spaces
printf("\n")
Ends the current line
After the inner loop
Print spaces and digits without a newline, then end the row once.
Try it
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 result5 rows · 25 cells
1
21
321
4321
54321
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and list spaces vs digits (spaces shown as · below for clarity).
i
Inner j = 4..1
Printed row
1
···1
1
2
··21
21
3
·321
321
4
4321
4321
Each of n rows prints n characters → n² cells total. That square count is why time is O(n²).
Code
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;
}
Output
1
21
321
4321
54321
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;
}
Output (when user enters 4)
Enter rows (1-9): 4
1
21
321
4321
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;
}
Output
1
21
321
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.
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.