Shape Rule
i..1 per row
Row $i prints digits $i, …, 1 with no spaces (e.g. row 3 → 321).

The Growing Reverse Number Triangle prints 1, 21, 321, 4321, 54321 — row $i concatenates digits $i down to 1. This tutorial covers nested-loop logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
i..1 per row
Row $i prints digits $i, …, 1 with no spaces (e.g. row 3 → 321).
$i = 1..$rows
Outer loop picks row $i; inner loop runs $j = $i..1.
No spaces
echo $j concatenates digits on the same row.
Series base
Stepping stone toward Floyd’s triangle, stars, and alphabet patterns.
1–20 rows
Pick a row count and draw the ascending triangle instantly in the browser.
Complexity
Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
A growing reverse number triangle pattern grows each row by one digit on the left: 1, 21, 321, up to 54321 for five rows. Row $i starts at $i and counts down to 1.
In PHP the outer loop runs $i = 1..$rows, the inner loop prints $j from $i down to 1, then echo PHP_EOL moves to the next line.
It is a foundational nested-loop exercise — compare with Program 4 (fixed prefix descending) and continue to Program 8 (reverse growing number pattern).
Each row begins at digit $i and prints down to 1.
Inner loop $j = $i..1 lengthens each row.
Program 5 inner loop ascends $j = 1..$i; Program 7 counts $j = $i..1 on each row.
Compare Program 5 (ascending) and Program 6 (suffix); continue to Program 8 next.
In short: for each $i from 1 to $rows, print $j from $i down to 1, then echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print a growing reverse Number Triangle: row $i prints digits $i down to 1 with no spaces (e.g. row 3 → 321).
// $rows = 5 (conceptual shape)
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j >= 1; $j--) {
echo $j; // digits i..1
}
echo PHP_EOL; // next row
} | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle lines — outer loop runs from 1 up to $rows. |
$i | int | Outer loop — current row index; sets where the inner loop stops. |
$j | int | Inner loop — descending from $i down to 1. |
for i from 1 to rows:
for j from i down to 1:
print j
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 1, 21, 321, … | Learning and interviews |
| User-input rows | fgets(STDIN); | Flexible console programs |
| Spaced output | echo $j . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = 1; $i <= $rows; $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 | fgets(STDIN); |
| Program 4 contrast | Program 4 uses $j = $rows..$i (fixed prefix); Program 7 uses $j = $i..1 |
How outer row selection and inner $j = $i..1 work together.
for ($i = 1; $i <= $rows; $i++)Picks row number $i — triangle height.
for ($j = $i; $j >= 1; $j--)Prints exactly $i digits on row $i.
echo $jConcatenate digits with no spaces — 123 not 1 2 3.
trace i=3Dry-run row 3: $j = 3..1 → prints 321.
Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.
Natural follow-up after left-aligned descending triangles — now the prefix grows from 1.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 4 (descending prefix), then continue to Program 8 (reverse growing number pattern).
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.
Enter a row count and draw the Growing Reverse Number Triangle in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows — inner loop prints $i..1 on each line.
$rows = 5Hard-coded row count — inner loop prints from $i down to 1.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j >= 1; $j--) {
echo $j;
}
echo PHP_EOL;
} When $i = 3, the inner loop prints 3, 2, 1 — output 321. When $i = 5, output is 54321.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read $rows with fgets(STDIN); same nested loops as Example 1.
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j >= 1; $j--) {
echo $j;
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes.
Add spaces between digits for easier reading.
Print a space after each digit with echo $j . " ".
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j >= 1; $j--) {
echo $j . " ";
}
echo PHP_EOL;
} Same $j = $i..1 logic; only the output format adds spaces between digits.
echo is built in; use fgets(STDIN) when reading input. Set loop variables $i, $j with $rows = 5.
for ($i = 1; $i <= $rows; $i++) — each row adds one more digit.
for ($j = $i; $j >= 1; $j--) — prints digits from $i down to 1.
echo PHP_EOL ends the row after the inner loop finishes.
Each row grows by one digit — O(n²) time, O(1) extra memory.
$i = 3Trace row 3 to see how the inner loop builds 321.
Row $i | $j range | Output |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2, 1 | 21 |
| 3 | 3, 2, 1 | 321 |
| 4 | 4 … 1 | 4321 |
| 5 | 5 … 1 | 54321 |
After the inner loop, echo PHP_EOL moves to the next row.
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 compare with Program 5 and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 8 for the reverse growing pattern.
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 fgets(STDIN) 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 PHP 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 grows by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts up; inner loop must run $j = $i..1 for the ascending prefix.
fgets(STDIN)Call is_numeric($input) so bad input does not leave $rows uninitialized.
echo PHP_EOL OutsideOnly call echo PHP_EOL after the inner loop finishes the row.
echo $j for Digitsfor ($j = $i; $j >= 1; $j--) echo $j concatenates digits on one line.
$rows = 3Trace $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 echo PHP_EOL inside the inner loop.
Mistakes that commonly break Growing Reverse Number Triangle patterns.
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.
Every row prints $rows digits — you get a rectangle, not a growing triangle.
→ Keep for ($j = $i; $j >= 1; $j--) so row length equals $i.
for ($j = $i; $j >= 1; $j--) reverses digit order — still a triangle, but not 1, 21, 321.
→ Use ascending inner loop $j = $i..1 for the standard pattern.
$j = $rows..$i produces Program 4’s fixed-prefix shape — not this pattern.
→ Use $j = $i..1 here — Program 4 uses $j = $rows..$i.
Letters or empty input leave $rows uninitialized.
→ Call is_numeric($input) 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: 1 and 21.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Each row prints $i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
54321, 5432…for ($i = $rows; $i >= 1; $i--)5, 54, 543…echo $j . " " between digits$i = 1..$rows. Inner loop: $j = $i..1 with echo $j.echo 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 4 where each row prints $rows - $i + 1 digits.Quick Takeaway: outer loop $i = 1..$rows, 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) |
| Spaced output (Example 3) | O(n²) | O(1) |
The Growing Reverse Number Triangle is a compact nested-loop lesson: outer loop grows rows while the inner loop prints digits $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 8 for the reverse growing number pattern (5, 54, 543, 5432, 54321).
Row $i prints $i..1 — keep echo $j for digits and echo PHP_EOL for the row break.
for ($i = 1; $i <= $rows; $i++) in the outer loopfor ($j = $i; $j >= 1; $j--) prints digits i..1echo $j for digits and echo PHP_EOL after each row$rows ≥ 1 for interactive programsis_numeric($input) before using $rowsecho PHP_EOL inside the inner digit loopj++ from 1 when you meant Program 5 — this pattern needs j-- from $iecho PHP_EOL inside the inner loop — digits must stay on one row$rows = 1 edge casePrint the pattern the beginner-friendly way.
Row $i prints $i..1
Counts up $rows
$j = $i down to 1
Ends each row
ShapeO(n²) time
AnalysisOuter loop grows row index $i; inner loop prints $j from $i down to 1 — 1, 21, 321, … O(n²) for $n rows.
Move on to the reverse growing number pattern in the PHP number-pattern series.
12 people found this page helpful