A descending number triangle prints digits 1..i on each row, with the outer loop counting down so the first row is longest.
Remember
Rule: for i = n..1, print 1..i
12345
1234
123
12
1 ← 5 rows
In C you solve it with two nested for loops: the outer loop picks the row length, the inner loop prints digits with printf("%d", j), then printf("\n") ends the row. Flip the outer loop upward for an ascending triangle (Program 5).
Approach
How to Solve It
Outer loop counts down from rows to 1. Inner loop prints digits 1..i. End each row with printf("\n").
Method
Idea
Best for
Nested loops
Outer = length n..1, inner = digits 1..i via printf
Learning, interviews, exams
Spaced digits
printf("%d ", j) instead of printf("%d", j)
Easier reading on each row
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Shrink each row
for (i = rows; i >= 1; i--)
Print digits 1..i
for (j = 1; j <= i; j++) printf("%d", j);
End the row
printf("\n");
Spaced form
printf("%d ", j);
Ascending instead
for (i = 1; i <= rows; i++) with the same inner loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j)
Stays on the same line
Each digit j
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 descending triangle updates instantly — capped at 9 so every digit 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
12345
1234
123
12
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and the digits the inner loop prints.
i
Inner j
Printed row
Digits
4
1..4
1234
4
3
1..3
123
3
2
1..2
12
2
1
1..1
1
1
Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.
Code
C Programs
Three complete programs: fixed height, scanf input, and a spaced-digit variant. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop shrinks the length; inner loop prints digits 1..i.
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("%d", j);
printf("\n");
}
return 0;
}
Output
12345
1234
123
12
1
How It Works
1. Shrink the count.for (i = rows; i >= 1; i--) yields 5, 4, 3, 2, 1.
2. Print digits. When i = 5, the inner loop prints 12345; when i = 1, just 1.
3. Newline.printf("\n") after the inner loop starts the next shorter row.
Example 2 — User Input
Read the row count with scanf and reject non-positive values.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Please enter a positive integer.\n");
return 1;
}
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1234
123
12
1
How It Works
1. Validate rows. Require scanf success and rows >= 1.
2. Same nested loops. Four rows start at 1234 and end at 1.
3. Safer demos. Cap at single-digit rows if you want a tight look:
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
Same loops with printf("%d ", j) so digits are easier to read.
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("%d ", j);
printf("\n");
}
return 0;
}
Output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
How It Works
1. Same structure. Only the format string changes — loops stay identical to Example 1.
2. Space after each digit.printf("%d ", j) separates values for clearer scanning.
3. When to use it. Prefer the tight form for classic screenshots; use spaced form when teaching or tracing by hand.
Edge Cases & Pitfalls
Check these before calling the solution done.
grow not shrink
for (i = 1; i <= rows; i++)
That outer loop grows the triangle (1, 12, 123…). Keep i = rows; i >= 1; i-- for this pattern.
\n inside
printf("\n") inside the inner loop
That puts each digit on its own line. Print digits without a newline; call printf("\n") only after the row finishes.
off-by-one
j < i instead of j <= i
You drop the last digit on every row (e.g. 1234 becomes 123).
rows = 1
Single 1
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
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(n²)
O(1)
Spaced digits (Example 3)
O(n²)
O(1)
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = n..1, print digits 1..i with printf.
Count down: the outer loop starts at rows so the first line is longest.
printf vs newline: digits stay on the line; printf("\n") advances after each row.
Next step: Program 2 shifts the start digit left each row.
One line: for each length from n down to 1, print 1..i with printf, then printf("\n").
Frequently Asked Questions
The outer loop runs i from rows down to 1. For each row i, the inner loop runs j from 1 to i and prints j with printf. Row rows prints 1..rows, the next row prints one fewer digit, down to a single 1.
Counting down makes the first row the longest. for (i = rows; i >= 1; i--) sets i to the full width first, then shrinks by one each line — matching 12345, 1234, 123, 12, 1.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf without a newline; the row break uses printf("\n") after the inner loop.
Change the outer loop to for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++) printf("%d", j). The first row then has one digit and the last row has rows digits — that is Program 5.
O(n²) where n is the number of rows. Total digit prints equal 1+2+…+n = n(n+1)/2.
Yes. putchar('0' + j) writes one digit when j is 1..9. Nested loops with putchar are a common, slightly leaner style; keep printf if your course examples use it.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Row i prints digits 1 through i. The outer loop counts down from rows, so the first line is longest and each row shortens by one digit — still O(n²) total prints. This is the classic first console pattern in C labs.