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 PHP 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 = $rows; $i >= 1; $i--) shrinks the row length each iteration.
i..1 descending
for ($j = $i; $j >= 1; $j--) prints digits in reverse order on each row.
Same line / next line
Digits use echo $j; end each row with echo PHP_EOL.
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 PHP the outer loop runs $i = $rows..1, the inner loop prints $j from $i down to 1, then echo PHP_EOL 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 echo PHP_EOL.
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)
// 54321
// 4321
// 321
// 21
// 1 | 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) trim(fgets(STDIN)); | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = $rows; $i >= 1; $i--) |
| Print digits $i..1 | for ($j = $i; $j >= 1; $j--) echo $j; |
| End the row | echo PHP_EOL; |
| Spaced digits | echo $j . " "; |
| User input | (int) trim(fgets(STDIN)); |
| Program 2 contrast | for ($i = 1; $i <= $rows; $i++) 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 fgets(STDIN) 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 PHP 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.
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j >= 1; $j--) {
echo $j;
}
echo PHP_EOL;
} 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 fgets(STDIN) instead of hard-coding 5.
Read $rows with (int) trim(fgets(STDIN)); both loops use $rows as the starting bound.
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j >= 1; $j--) {
echo $j;
}
echo PHP_EOL;
} Same descending-loop core as Example 1; only the source of $rows changes from a literal to user input. Non-numeric input leaves $rows unset if you skip is_numeric() checks — always validate CLI input in safer labs.
Add a space between digits for easier reading on each row.
Keep $rows = 5 but print each digit followed by a space.
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--) {
for ($j = $i; $j >= 1; $j--) {
echo $j . " ";
}
echo PHP_EOL;
} Only the print statement changes — echo $j . " " instead of echo $j. Loop bounds stay the same as Example 1.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = $rows; $i >= 1; $i--) — descending outer loop shrinks each row.
for ($j = $i; $j >= 1; $j--) — prints digits $i..1 in reverse order.
echo PHP_EOL 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 echo vs echo PHP_EOL without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use echo $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 is_numeric() 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 (i--); inner loop must also count down from $i to 1.
trim(fgets(STDIN))Call is_numeric(trim($line)) so bad input does not leave $rows uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
for ($j = $i; $j >= 1; $j--) 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 echo PHP_EOL 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 echo $j for digits; echo PHP_EOL 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 = $i; $j >= 1; $j--) so each row reads $i..1.
for ($i = 1; $i <= $rows; $i++) grows rows instead of shrinking them.
→ Use for ($i = $rows; $i >= 1; $i--) 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: $j = $i.
Letters or empty input leave $rows uninitialized.
→ Call is_numeric(trim($line)) 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.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric() first.
Each row prints $i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
$rows each rowecho $j . " " between digits$i = $rows..1. Inner loop: $j = $i..1 with j--.echo.print stays on the line; echo PHP_EOL 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 echo $j, then echo PHP_EOL.
| 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 echo $j for digits and echo PHP_EOL for the break, and validate row counts when reading input.
for ($i = $rows; $i >= 1; $i--) in the outer loopfor ($j = $i; $j >= 1; $j--) prints digits in reverseecho $j for digits and echo PHP_EOL after each rowrows ≥ 1 for interactive programsis_numeric(trim($line)) before using $rowsecho PHP_EOL inside the inner digit looprows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints i..1
DefinitionCounts down rows
Code$j = $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 PHP number-pattern series.
12 people found this page helpful