A fill-with-n number triangle prints an ascending run from the row start to $n, then pads with $n so every row has the same width.
Remember
Rule: for i = n..1, print i..n then pad with n (i-1 times)
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5 ← n = 5
In PHP two inner loops do the work: sequence ($j from $i to $n) then fill ($n printed $i - 1 times), then echo PHP_EOL ends the row. Program 20 continues with a continuous counter across rows.
Approach
How to Solve It
Outer loop counts down from $n to 1. First inner loop prints $i..$n. Second inner loop pads with $n. End each row with echo PHP_EOL.
Method
Idea
Best for
Sequence + fill
Print $i..$n, then pad with $n
Learning, interviews, exams
Custom fill
Pad with a separate constant
When the fill value should differ from $n
Pseudocode
Pseudocode
for i from n down to 1:
for j from i to n:
print j and a space
for j from 1 to i - 1:
print n and a space
print newline
Cheat sheet
Goal
Pattern
Outer (descend)
for ($i = $n; $i >= 1; $i--)
Sequence
for ($j = $i; $j <= $n; $j++) echo $j . " ";
Fill pad
for ($j = 1; $j < $i; $j++) echo $n . " ";
End the row
echo PHP_EOL;
Custom fill
Print $fill instead of $n in the second loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j . " "
Stays on the same line
Each number in the row
echo PHP_EOL
Ends the current line
After both inner loops
Print all numbers without a newline, then end the row once.
Try it
Live Preview
Change the width n and the fill-with-n triangle updates instantly.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · 5 rows · 25 numbers
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
Trace
Worked Walkthrough
Trace three outer values when $n = 5 — watch the sequence grow and the fill count shrink.
$i
Sequence
Fill count
Printed row
5
5
4
5 5 5 5 5
3
3 4 5
2
3 4 5 5 5
1
1 2 3 4 5
0
1 2 3 4 5
Every row prints exactly n numbers, so total work is n × n = n².
Code
PHP Programs
Three complete programs: fixed $n = 5, CLI input, and a custom fill constant. Use View Output for sample results.
Example 1 — Fixed $n = 5
Hard-coded width — sequence loop first, then pad with $n.
1. Same sequence. The first inner loop still prints $i..$n.
2. Different pad. The second loop prints $fill instead of $n.
3. Same width. Each row still has exactly $n numbers.
Edge Cases & Pitfalls
Check these before calling the solution done.
missing fill
Forgetting the second inner loop
Rows become jagged. Keep the pad loop so every row has width $n.
wrong fill count
$j <= $i instead of $j < $i
That prints one extra pad. Use for ($j = 1; $j < $i; $j++) for exactly $i - 1 fills.
PHP_EOL inside
echo PHP_EOL inside an inner loop
That puts each number on its own line. Call echo PHP_EOL only after both inner loops finish.
n = 1
Single number
Output is just 1 — a good sanity check.
n ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
spaces
Keep the trailing space
The classic sample uses echo $j . " " so numbers stay spaced and readable.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input / custom fill (Examples 1–3)
O(n²)
O(1)
There are n rows printing n numbers each, so total work is n².
Remember
Key Takeaways
Rule: for $i = n..1, print $i..$n, then pad with $n exactly $i - 1 times.
Two loops: sequence grows as you go down; fill shrinks by one each row.
echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after both inner loops.
Next step: Program 20 prints a continuous counter across growing rows.
One line: descend $i from $n, print $i..$n, pad with $n, then echo PHP_EOL.
Frequently Asked Questions
When $i = 5, the sequence loop prints $j = 5 once, then the fill loop runs 4 times — all values are 5, so the row is 5 5 5 5 5.
The first inner loop prints the increasing sequence $i..$n. The second fills remaining positions with $n so every row has the same width.
When $i = 1, the sequence loop prints $j = 1 to 5 and the fill loop runs zero times — no padding needed.
echo $j . " " stays on the same line with a trailing space. echo PHP_EOL ends the current line. Numbers use echo; the row break uses echo PHP_EOL after both inner loops.
Replace n in the second loop with a separate fill constant — Example 3 on this page pads with 9 while the sequence still runs to 5.
Rows will have different widths — the top row may be short while the bottom row is full length.
O(n²) for width n because each of n rows prints n numbers.
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?
Each row prints an ascending sequence $i..$n, then pads with $n so every row has width $n. The second inner loop runs $i - 1 times — still O(n²) total prints.