A number diamond prints ascending digits in odd-length rows that grow to a peak, then shrink back — centered with leading spaces.
Remember
Rule: top 1..levels, bottom levels-1..1; each row prints 1..(2*i-1)
1
123
12345
1234567
123456789
1234567
12345
123
1 ← levels = 5
In PHP use two outer loops: grow $i to $levels, then shrink. Indent with spaces, then print $k from 1 while $k < $i * 2.
Approach
How to Solve It
Build the top half, then mirror it downward — spaces first, then digits.
Method
Idea
Best for
Two-half diamond
Top 1..$levels, bottom $levels-1..1
Learning, interviews, exams
Levels input
Same logic with a user-chosen $levels
Practice / demos
Pseudocode
Pseudocode
for i from 1 to levels:
print (levels - i) spaces
print digits 1 to (2*i - 1)
print newline
for i from levels-1 down to 1:
print (levels - i) spaces
print digits 1 to (2*i - 1)
print newline
Cheat sheet
Goal
Pattern
Top half
for ($i = 1; $i <= $levels; $i++)
Bottom half
for ($i = $levels - 1; $i >= 1; $i--)
Top indent
for ($j = $i; $j < $levels; $j++) echo " ";
Bottom indent
for ($j = $levels; $j > $i; $j--) echo " ";
Digits
for ($k = 1; $k < $i * 2; $k++) echo $k;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo " " / echo $k
Stays on the same line
Each space or digit
echo PHP_EOL
Ends the current line
After spaces and digits on a row
Print spaces and digits without a newline, then end the row once.
Try it
Live Preview
Change the level count and the number diamond updates instantly.
Whole numbers from 1 to 5 (peak stays within single digits 1–9). Tap a chip or type a value — the preview redraws as you go.
Live result5 levels · 9 lines
1
123
12345
1234567
123456789
1234567
12345
123
1
Trace
Worked Walkthrough — $levels = 3
Trace spaces and digits on the way up, then back down.
Half
$i
Spaces / digits
Printed row
Top
1
2 / 1
1
Top
2
1 / 123
123
Top
3
0 / 12345
12345
Bottom
2
1 / 123
123
Bottom
1
2 / 1
1
Total lines = 2 * $levels - 1. Digit count on row $i = 2 * $i - 1.
Code
PHP Programs
Three complete programs: fixed 5 levels, CLI input levels, and a compact 3-level demo. Use View Output for sample results.
Example 1 — Fixed $levels = 5
Top half grows to 123456789; bottom half mirrors back to 1.
1. Same rules. Grow, then shrink; spaces then digits on every row.
2. Quick check. Five lines total; the middle row is 12345.
Edge Cases & Pitfalls
Check these before calling the solution done.
peak twice
Bottom loop starts at $levels
The widest row prints twice. Start the bottom at $levels - 1.
no spaces
Forget the space loops
Rows left-align and the diamond shape disappears. Keep $levels - $i spaces.
$k <= $i
Print only to $i
Rows become 1, 12, 123… instead of odd lengths. Use $k < $i * 2 (or $k <= 2*$i - 1).
levels > 5
Digits past 9
echo $k prints 10, 11… and columns break. Cap at 5 for single-digit rows, or format with fixed width.
levels = 1
Single cell
Output is just 1 — bottom loop never runs.
Bad input
(int) alone is not enough
Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
There are 2n - 1 rows and up to O(n) digits per row, so total work is quadratic in the level count.
Remember
Key Takeaways
Rule: grow $i to $levels, then shrink; each row prints 1..(2*$i-1).
Center: print $levels - $i spaces before the digits on every row.
echo vs PHP_EOL: spaces and digits stay on the line; echo PHP_EOL advances after each row.
Next step: Program 45 prints an X of stars and zeros on a grid.
One line: indent, print an odd-length ascending digit row, grow to the peak, then mirror down.
Frequently Asked Questions
A centered diamond of ascending digits: 1, 123, 12345, 1234567, 123456789, then the same rows mirrored back down to 1.
The row prints 2*$i-1 digits (odd length): 1, 3, 5, 7, 9, … using for ($k = 1; $k < $i*2; $k++).
A space loop prints leading spaces before the digits. Top half uses for ($j = $i; $j < $levels; $j++); bottom half uses for ($j = $levels; $j > $i; $j--).
The first builds the top half ($i = 1..$levels). The second mirrors back down ($i = $levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Replace 5 with a $levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) rows overall.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single centered 1.
🤔
Did you know?
Top half runs $i = 1..$levels; bottom half mirrors $i = $levels-1..1. Each row prints 2*$i-1 digits (1 to 2*$i-1) with leading spaces to center the diamond.