Shape Rule
rows..i per row
Row 1 prints 54321, row 2 prints 5432, shrinking until a single 5 — always starting at rows.

The left-aligned descending number triangle prints 54321, 5432, 543, 54, 5 — a natural step after Program 3’s reverse descending triangle. This tutorial covers ascending outer loop with shrinking inner bound, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
rows..i per row
Row 1 prints 54321, row 2 prints 5432, shrinking until a single 5 — always starting at rows.
i = 1..rows
for (i = 1; i <= rows; i++) increases the inner-loop stop value each iteration.
rows..i descending
for (j = rows; j >= i; j--) prints from the maximum digit down to i.
Same line / next line
Digits use printf("%d", j); end each row with printf("\n").
3–9 rows
Pick a row count and draw the left-aligned descending triangle in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
A left-aligned descending number triangle prints each row from rows down to i while the outer loop increases the stop value. With rows = 5, the output is 54321, 5432, 543, 54, 5.
In C the outer loop runs i = 1..rows, the inner loop prints j from rows down to i, then printf("\n") moves to the next line.
It teaches variable inner-loop stop conditions — a key step after Program 3’s reverse descending rows.
Inner bound stops at i.
Every row begins at rows.
Program 3 starts each row at i; Program 4 always starts at rows.
Follow Program 3; continue to Program 5 next.
In short: outer i = 1..rows, inner j = rows..i, printf("%d", j), then printf("\n").
Given a positive integer rows (e.g. 5), print a left-aligned descending triangle: each row prints digits from rows down to i, with the outer loop counting from 1 up to rows.
// rows = 5
//54321
//5432
//543
//54
//5 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines — also the maximum digit and first digit on every row. |
i | int | Outer loop — current row index; inner loop stops at i. |
j | int | Inner loop — descending from rows down to i. |
for i from 1 to rows:
for j from rows down to i:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 54321, 5432, … | Learning and interviews |
| User-input rows | scanf("%d", &rows); | Flexible console programs |
| Spaced output | printf("%d ", j) | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Print digits rows..i | for (j = rows; j >= i; j--) printf("%d", j); |
| End the row | printf("\n"); |
| Spaced digits | printf("%d ", j); |
| User input | scanf("%d", &rows); |
| Program 3 contrast | for (i = rows; i >= 1; i--) with j = i..1 |
Same left-aligned descending triangle — different ways to control rows and formatting.
i = 1..rowsIncreases stop value each line
j = rows..iDescending digits per row
rowsFirst digit every row
j--Inner loop must count down
Reach for this pattern when teaching descending inner loops and shrinking row lengths.
Natural follow-up after Program 3 — same shrinking rows but every line starts at rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with scanf for a flexible row count.
Compare Program 3 (reverse descending) and Program 5 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the left-aligned descending triangle in the browser.
Three complete C programs — fixed rows, user input, and spaced output. Click View Output to reveal sample console results.
Print five rows of the left-aligned descending triangle with nested loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= i; j--)
printf("%d", j);
printf("\n");
}
return 0;
} When i = 1, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When i = 5, only one digit prints — output 5. The outer loop increases i each row, shortening the inner loop.
Read the row count with scanf instead of hard-coding 5.
Read rows with scanf("%d", &rows).
#include <stdio.h>
int main() {
int rows;
int i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = rows; j >= i; j--)
printf("%d", j);
printf("\n");
}
return 0;
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each digit followed by a space.
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= i; j--)
printf("%d ", j);
printf("\n");
}
return 0;
} Only the print statement changes — printf("%d ", j) instead of printf("%d", j). Loop bounds stay the same as Example 1.
#include <stdio.h> brings in printf / scanf. Set loop variables i, j with rows = 5.
for (i = 1; i <= rows; i++) — ascending outer loop increases the inner-loop stop value each row.
for (j = rows; j >= i; j--) — prints digits rows..i in descending order.
printf("\n") ends the row after the inner loop finishes.
Rows shrink from rows digits to one — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i, the inner-loop range, digit count, and full row output.
i | Inner loop (j) | Prints | Row output |
|---|---|---|---|
1 | 5, 4, 3, 2, 1 | 5 | 54321 |
2 | 5, 4, 3, 2 | 4 | 5432 |
3 | 5, 4, 3 | 3 | 543 |
4 | 5, 4 | 2 | 54 |
5 | 5 | 1 | 5 |
Prints per row = rows - i + 1 — total prints = n(n+1)/2 for n rows.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: flip j-- to j++ and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 5 for the next pattern in the series.
Practice printf vs printf("\n") without complex math.
Example: put printf("\n") inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use printf("%d ", j) between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — total is 15 (5+4+3+2+1).
Pair the pattern with scanf return checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row shortens by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts up (i++); inner loop counts down from rows to i.
scanf return valueAvoid crashes when the user types letters instead of a number.
Only call printf("\n") after the inner loop finishes the row.
for (j = rows; j >= i; j--) prints digits rows..i in descending order.
Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put printf("\n") inside the inner loop.
Mistakes that commonly break left-aligned descending number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use printf("%d", j) for digits; printf("\n") only after the inner loop.
for (j = 1; j <= i; j++) prints ascending digits — you get Program 1’s shape, not this one.
→ Keep for (j = rows; j >= i; j--) so each row reads rows..i.
for (i = rows; i >= 1; i--) with j = i..1 prints Program 3’s reverse descending triangle instead.
→ Use for (i = 1; i <= rows; i++) with inner j = rows..i for this pattern.
for (j = rows; j >= 1; j--) on every row prints the full line 54321 repeatedly.
→ Stop the inner loop at the current outer value: j >= i, not j >= 1.
Letters or empty input leave rows uninitialized or unchanged.
→ Check scanf return value and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 21 and 2.
scanf without a return check is unsafe — validate input.
Each row prints rows - i + 1 digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
printf("%d ", j) between digitsi = 1..rows. Inner loop: j = rows..i with j--.printf stays on the line; printf("\n") advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly rows - i + 1 digits — compare with Program 3 where each row prints i digits starting at i.Quick Takeaway: outer loop i = 1..rows, inner loop j = rows..i with printf("%d", j), then printf("\n").
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The left-aligned descending number triangle is a compact nested-loop lesson: an ascending outer loop raises the inner stop each row while the inner loop always starts at rows and counts down to i. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 5 for the next pattern in the series.
Row i prints rows..i — keep printf("%d", j) for digits and printf("\n") for the break, and validate row counts when reading input.
for (i = 1; i <= rows; i++) in the outer loopfor (j = rows; j >= i; j--) prints digits from rows down to iprintf("%d", j) for digits and printf("\n") after each rowrows ≥ 1 for interactive programsscanf return value instead of ignoring bad inputprintf("\n") inside the inner digit looprows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints rows..i
DefinitionCounts up rows
Codej = rows down to i
Codeprintf("\n") after inner loop
ShapeO(n²) time
AnalysisEach row starts at rows and counts down to i. Row i prints rows - i + 1 digits — total prints = n(n+1)/2; output is left-aligned with no leading spaces.
Move on to the next pattern in the C number-pattern series.
12 people found this page helpful