An inverted repeating number triangle prints digit i repeated exactly i times, counting i down from rows.
Remember
Rule: for i = n..1, print i exactly i times
55555
4444
333
22
1 ← 5 rows
In PHP you solve it with two nested for loops: the outer loop picks the digit, the inner loop repeats it with echo $i, then echo PHP_EOL ends the row. Program 9 is the ascending twin (1, 22, 333…); Program 10 grows repeats while the digit falls (5, 44, 333…).
Approach
How to Solve It
Outer loop counts down from $rows to 1. Inner loop repeats digit $i exactly $i times. End each row with echo PHP_EOL.
Method
Idea
Best for
Nested loops
Outer picks digit n..1; inner repeats it i times
Learning, interviews, exams
str_repeat
Build each full row in one call
Leaner demos once loops click
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print i (no newline)
print newline
Inner bound:$j from 1 to $i so digit and count stay equal.
echo vs PHP_EOL: repeats stay on the line; echo PHP_EOL advances after each row.
Next step: Program 12 shrinks while counting up (11111, 2222, 333…).
One line: for each digit from n down to 1, print it i times with echo, then echo PHP_EOL.
Frequently Asked Questions
The outer loop decreases i from rows down to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times, so the widest row is first and each next row is shorter.
for ($j = 1; $j <= $i; $j++) echo $i runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
echo stays on the same line. echo PHP_EOL ends the current line. Repeated digits use echo $i; the row break uses echo PHP_EOL after the inner loop.
Program 10 grows repeat count as the digit shrinks (5, 44, 333, 2222, 11111) using for ($j = $rows; $j >= $i; $j--). Program 11 repeats digit i exactly i times (55555, 4444, 333, 22, 1) using for ($j = 1; $j <= $i; $j++).
O(n²) where n is the number of rows. Total echo calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. echo str_repeat((string)$i, $i) . PHP_EOL prints a full row in one call. Nested loops are better for learning; str_repeat is a handy shortcut once the bounds make sense.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Row digit i repeats exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.