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.
Approach
How to Solve It
Start with fixed rows = 5, then generalize with scanf — optionally space the digits for easier reading.
Method
Idea
Best for
Double countdown
Outer i = rows..1; inner j = i..1
Learning, interviews, exams
Spaced digits
Same 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
Goal
Pattern
Shrink rows downward
for (i = rows; i >= 1; i--)
Print i..1
for (j = i; j >= 1; j--) printf("%d", j);
End the row
printf("\n");
Spaced variant
printf("%d ", j);
Ascending digits instead
for (j = 1; j <= i; j++) printf("%d", j);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j)
Stays on the same line
Each digit in the row
printf("\n")
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
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 result5 rows · 15 digits
54321
4321
321
21
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i from 4 down to 1 and list the digits printed.
i
Inner j = i..1
Printed row
4
4, 3, 2, 1
4321
3
3, 2, 1
321
2
2, 1
21
1
1
1
Total digits for n rows = n(n + 1)/2 (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).
Code
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;
}
Output
54321
4321
321
21
1
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;
}
Output (when user enters 4)
Enter rows (1-9): 4
4321
321
21
1
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;
}
Output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.