Shape Rule
i..1 per row
Row 1 prints 54321, row 2 prints 4321, shrinking until a single 1.

The reverse descending number triangle prints 54321, 4321, 321, 21, 1 — a natural step after the left-shifted triangle in Program 2. This tutorial covers descending outer and inner loops, a live preview, algorithm steps, worked Python examples, edge cases, and complexity.
i..1 per row
Row 1 prints 54321, row 2 prints 4321, shrinking until a single 1.
rows..1
for i in range(rows, 0, -1): shrinks the row length each iteration.
i..1 descending
for j in range(i, 0, -1): prints digits in reverse order on each row.
Same line / next line
Digits use print(j, end=""); end each row with print().
3–9 rows
Pick a row count and draw the reverse descending triangle in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
A reverse descending number triangle prints each row from i down to 1 while the outer loop shrinks the row length. With rows = 5, the output is 54321, 4321, 321, 21, 1.
In Python the outer loop runs i = rows..1, the inner loop prints j from i down to 1, then print() moves to the next line.
It teaches descending inner loops — a key step after Program 2’s left-shifted ascending rows.
Outer loop i = rows..1 shortens each row.
Inner loop j = i..1 counts downward.
Program 2 ascends i..rows; Program 3 descends i..1.
Follow Program 2; continue to Program 4 (left-aligned descending) next.
In short: for each i from rows down to 1, print j from i down to 1, then print().
Given a positive integer rows (e.g. 5), print a reverse descending triangle: each row i shows digits from i down to 1, with the outer loop counting from rows down to 1.
# rows = 5 (conceptual shape)
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end="") # digits i..1 on this row
print() # next row | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines — outer loop runs from rows down to 1. |
i | int | Outer loop — current row limit; also the first digit printed. |
j | int | Inner loop — descending from i down to 1. |
for i from rows down to 1:
for j from i down to 1:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 54321, 4321, … | Learning and interviews |
| User-input rows | int(input(...)) | Flexible console programs |
| Spaced output | print(j, end=" ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for i in range(rows, 0, -1): |
| Print digits i..1 | for j in range(i, 0, -1): print(j, end="") |
| End the row | print() |
| Spaced digits | print(j, end=" ") |
| User input | int(input(...)) |
| Program 2 contrast | for i in range(1, rows + 1): with j = i..rows |
Same reverse descending triangle — different ways to control rows and formatting.
i = rows..1Shrinks row length each line
j = i..1Descending digits per row
i = rowsLongest row on top
j--Inner loop must count down
Reach for this pattern when teaching descending inner loops and shrinking row lengths.
Natural follow-up after Program 2 — introduces a descending inner loop.
Outer/inner bound practice with an immediate visual check.
Combine loops with input() for a flexible row count.
Compare Program 2 (left-shifted) and Program 4 (left-aligned descending) 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 reverse descending triangle in the browser.
Three complete Python programs — fixed rows, user input, and spaced output variant. Click View Output to reveal sample console results.
Print five rows of the reverse descending triangle with nested descending loops.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
rows = 5
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print() When i = 5, the inner loop prints 5, 4, 3, 2, 1 — output 54321. When i = 1, only one digit prints — output 1. The outer loop shrinks i each row.
Read the row count with input() instead of hard-coding 5.
Read rows with input() and int() (wrap in try/except ValueError in real apps).
rows = int(input("Enter the number of rows: "))
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end="")
print() Same descending-loop core as Example 1; only the source of rows changes from a literal to user input. Non-numeric input raises ValueError with bare int(input()) — use try/except for safer labs.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each digit followed by a space.
rows = 5
for i in range(rows, 0, -1):
for j in range(i, 0, -1):
print(j, end=" ")
print() Only the print statement changes — print(j, end=" ") instead of print(j, end=""). Loop bounds stay the same as Example 1.
No imports needed for fixed rows; use input() when reading. Set rows = 5 and loop variables i, j.
for i in range(rows, 0, -1): — descending outer loop shrinks each row.
for j in range(i, 0, -1): — prints digits i..1 in reverse order.
print() 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 |
|---|---|---|---|
5 | 5, 4, 3, 2, 1 | 5 | 54321 |
4 | 4, 3, 2, 1 | 4 | 4321 |
3 | 3, 2, 1 | 3 | 321 |
2 | 2, 1 | 2 | 21 |
1 | 1 | 1 | 1 |
Prints per row = i — 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 4 for a left-aligned descending triangle.
Practice print vs row newline without complex math.
Example: put print() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use print(j, end=" ") 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 input() 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 down with range(rows, 0, -1); inner loop must also count down from i to 1.
try/except ValueErrorUse try/except ValueError so bad input does not crash when converting rows.
Only call print() after the inner loop finishes the row.
for j in range(i, 0, -1): prints digits i..1 in reverse order.
Trace i = 3, 2, 1 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 print() inside the inner loop.
Mistakes that commonly break reverse descending number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use print(j, end="") for digits; print() only after the inner loop.
for j in range(1, i + 1): prints ascending digits — you get Program 1’s shape, not this one.
→ Keep for j in range(i, 0, -1): so each row reads i..1.
for i in range(1, rows + 1): grows rows instead of shrinking them.
→ Use for i in range(rows, 0, -1): so the first row is the longest.
j = rows on every row prints the same full line repeatedly.
→ Start the inner loop at the current outer value: range(i, 0, -1).
int(input())Letters or empty input raise ValueError with bare int(input()).
→ Catch ValueError 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 1.
Bare int(input()) raises ValueError on bad input — use try/except first.
Each row prints i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
rows each rowprint(j, end=" ") between digitsi = rows..1. Inner loop: j = i..1 with j--.print(j, end="") stays on the line; print() advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly i digits — compare with Program 2 where each row prints rows - i + 1 digits.Quick Takeaway: outer loop i = rows..1, inner loop j = i..1 with print(j, end=""), then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The reverse descending number triangle is a compact nested-loop lesson: a descending outer loop shrinks each row while the inner loop prints digits from i down to 1. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 4 for the left-aligned descending number triangle.
Row i prints i..1 — keep print(j, end="") for digits and print() for the break, and validate row counts when reading input.
for i in range(rows, 0, -1): in the outer loopfor j in range(i, 0, -1): prints digits in reverseprint(j, end="") for digits and print() after each rowrows ≥ 1 for interactive programsint(input()) in try/except ValueErrorprint() inside the inner digit looprows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints i..1
DefinitionCounts down rows
Codej = i down to 1
CodeEnds each row
ShapeO(n²) time
AnalysisThis 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.
Move on to the left-aligned descending number triangle in the Python number-pattern series.
12 people found this page helpful