Shape Rule
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.

The number-star diamond prints 1, 2*2, 3*3*3, … 5*5*5*5*5, then mirrors back down — a natural step after the right-aligned triangle in Program 30. This tutorial covers two outer loops, modulus alternation, a live preview, worked PHP examples, edge cases, and complexity.
Diamond halves
Top half grows 1..n; bottom half mirrors n-1..1.
$i = 1..$n
for ($i = 1; $i <= $n; $i++) — builds the growing half of the diamond.
$i = $n-1..1
for ($i = $n - 1; $i >= 1; $i--) — mirrors the top half back down.
Alternate fill
Odd $j prints $i; even $j prints *.
Height 3–7
Pick a height and draw the number-star diamond in the browser.
Complexity
Each row prints 2*$i-1 chars — total work scales as n².
A number-star diamond pattern alternates the row number and * on each line, growing to a peak then mirroring back down. With $n = 5, you get 1, 2*2, … 5*5*5*5*5, then the same rows in reverse.
In PHP you use two outer loops (top and bottom halves) and $j % 2 inside the inner loop to alternate digit and star.
It combines symmetric diamond logic with the modulus operator — a step up from Program 30’s single-loop triangle.
Inner loop runs $j < $i*2.
Odd prints $i, even prints *.
Top 1..n, bottom n-1..1.
Follow Program 30; continue to Program 32 (triangle from 11) next.
In short: top loop $i = 1..$n, bottom loop $i = $n-1..1, inner $j % 2 alternates digit and star, then echo PHP_EOL.
Given $n = 5, print a number-star diamond: top half $i = 1..$n, bottom half $i = $n-1..1, each row alternating digit $i and * via $j % 2.
// $n = 5 (conceptual shape)
// 1
// 2*2
// 3*3*3
// 4*4*4*4
// 5*5*5*5*5
// 4*4*4*4
// 3*3*3
// 2*2
// 1 | Item | Type | Description |
|---|---|---|
$n | int | Diamond peak height — total lines = 2*n - 1. |
$i | int | Outer loop — current row number printed on odd positions. |
$j | int | Inner loop — $j % 2 == 0 prints *, else prints $i. |
for i from 1 to n:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline
for i from n-1 down to 1:
for j from 1 to i*2 - 1:
if j % 2 == 0: print *
else: print i
print newline | Approach | Idea | Best for |
|---|---|---|
| if/else | 1, 2*2, 3*3*3, … | Learning and interviews |
| Ternary operator | echo ($j % 2 == 0) ? "*" : $i | Compact console programs |
| User-input n | (int) trim(fgets(STDIN)); | Flexible diamond height |
| Goal | Pattern |
|---|---|
| Top half | for ($i = 1; $i <= $n; $i++) |
| Bottom half | for ($i = $n - 1; $i >= 1; $i--) |
| Inner loop | for ($j = 1; $j < $i * 2; $j++) |
| Alternate fill | if ($j % 2 == 0) echo "*"; else echo $i; |
| Ternary form | echo ($j % 2 == 0) ? "*" : $i; |
| User input | (int) trim(fgets(STDIN)); |
Same number-star diamond — different ways to write the modulus check and control height.
$i = 1..$nGrowing rows to the peak
$i = $n-1..1Mirror back down
$j%2==0 ? * : $iAlternate star and digit
2*$i-1Characters per row
Reach for this pattern when teaching symmetric diamonds, the modulus operator, and two-phase loop structures.
Natural follow-up after Program 30 — introduces modulus and a mirrored bottom half.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 30 (right-aligned triangle) and Program 32 (triangle from 11) 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 height between 3 and 7 and draw the number-star diamond in the browser.
Three complete PHP programs — fixed height, user input with ternary form, and a smaller trace demo. Click View Output to reveal sample console results.
Print a full diamond with $n = 5 using if/else and $j % 2.
$n = 5Hard-coded row count — ideal for first demos and screenshots.
<?php
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j < $i * 2; $j++) {
if ($j % 2 == 0) {
echo "*";
} else {
echo $i;
}
}
echo PHP_EOL;
}
for ($i = 4; $i >= 1; $i--) {
for ($j = 1; $j < $i * 2; $j++) {
if ($j % 2 == 0) {
echo "*";
} else {
echo $i;
}
}
echo PHP_EOL;
} When $i = 1, the inner loop prints one character — 1. When $i = 3, it prints 3*3*3 (five characters). The bottom half mirrors from $i = 4 down to 1.
Read the diamond height with fgets(STDIN) instead of hard-coding 5.
Read $n with (int) trim(fgets(STDIN)) to control diamond height.
<?php
echo "Enter n: ";
$n = (int) trim(fgets(STDIN));
if ($n < 1) return;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j < $i * 2; $j++) {
echo ($j % 2 == 0) ? "*" : $i;
}
echo PHP_EOL;
}
for ($i = $n - 1; $i >= 1; $i--) {
for ($j = 1; $j < $i * 2; $j++) {
echo ($j % 2 == 0) ? "*" : $i;
}
echo PHP_EOL;
} Same diamond core as Example 1; a ternary operator replaces if/else and $n replaces hard-coded 5. Non-numeric input leaves $n unset if you skip is_numeric() checks — always check it in safer labs.
Run with $n = 3 to trace every row on paper before scaling up.
$n = 3Same if/else logic with a smaller row count for quick tracing.
<?php
$n = 3;
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j < $i * 2; $j++) {
if ($j % 2 == 0) {
echo "*";
} else {
echo $i;
}
}
echo PHP_EOL;
}
for ($i = $n - 1; $i >= 1; $i--) {
for ($j = 1; $j < $i * 2; $j++) {
if ($j % 2 == 0) {
echo "*";
} else {
echo $i;
}
}
echo PHP_EOL;
} Only $n changes from 5 to 3 — the if/else and two-loop structure stay identical. Trace $i = 1, 2, 3 on paper to see how row length grows as 2*$i-1.
Set $n = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 1; $i <= $n; $i++) — growing rows from 1 to the peak.
for ($j = 1; $j < $i * 2; $j++) — prints 2*$i-1 characters per row.
$j % 2 == 0 prints *; odd $j prints $i.
for ($i = $n - 1; $i >= 1; $i--) — mirrors the top half back down.
2*n-1 total rows — O(n²) time, O(1) extra memory.
$n = 5Trace each outer-loop value of $i, inner-loop range, character count, and full row output.
$i | Inner range ($j) | Chars | Row output |
|---|---|---|---|
1 | 1 | 1 | 1 |
2 | 1, 2, 3 | 3 | 2*2 |
3 | 1..5 | 5 | 3*3*3 |
4 | 1..7 | 7 | 4*4*4*4 |
5 | 1..9 | 9 | 5*5*5*5*5 |
Characters per row = 2*$i-1. Bottom half repeats rows 4, 3, 2, 1 in reverse.
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 % 2 logic and watch stars land on wrong positions.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 32 for a triangle starting from 11.
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 $i . " " between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed characters for $n = 5 — top half alone prints 25 chars.
Pair the pattern with fgets(STDIN) return checks and positive-row checks.
Example: reject max <= 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 $n = 3 before coding — watch how row length grows as 2*$i-1.
Small habits that keep number-pattern code clean.
Top half 1..n and bottom half n-1..1 — do not repeat the peak row.
trim(fgets(STDIN))Call is_numeric(trim($line)) so bad input does not leave $n uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
Mark odd/even positions for each row before coding the alternation.
Trace $i = 1..3 on paper before coding the full $n = 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 number-star diamond patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $i or echo "*"; echo PHP_EOL only after the inner loop.
Using $j % 2 != 0 for stars (instead of == 0) swaps digit and star positions.
→ Even $j prints *; odd $j prints $i.
$j <= $i * 2 adds an extra character — row length becomes even instead of odd.
→ Keep for ($j = 1; $j < $i * 2; $j++) for exactly 2*$i-1 chars.
Starting the bottom loop at $i = $n prints the widest row twice.
→ Bottom half starts at $i = $n - 1, not $n.
Letters or empty input leave $n uninitialized.
→ Call is_numeric(trim($line)) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one row, no bottom half needed.
Outer loop never runs — print nothing or show a message.
$n < 0Treat as invalid; re-prompt instead of silent empty output.
Three rows: 1, 2*2, 1.
Unchecked CLI input leaves $n unset — call is_numeric() first.
Total lines = 2*n - 1 — grows quadratically with peak height.
Try these variations to lock in the pattern.
$j > $i$i = 1..$n without the mirror* with # or .$j % 2 logic, different symbol$j prints $i; even $j prints *. Inner loop runs $j < $i*2.echo.print stays on the line; echo PHP_EOL advances — mix them carefully.$n > 0 for interactive programs; $n = 1 prints a single 1.$n - 1 — do not repeat the peak row at $i = $n.Quick Takeaway: top loop $i = 1..$n, bottom $i = $n-1..1, inner $j % 2 alternates digit and star, 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 number-star diamond is a compact lesson in symmetric patterns and the modulus operator: alternate $i and * with $j % 2, grow rows in the top half, then mirror back down. Master the fixed-$n version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 32 for the increasing number triangle starting from 11.
Bottom half must start at $n - 1 — validate $n when reading from the console.
for ($i = 1; $i <= $n; $i++)for ($i = $n - 1; $i >= 1; $i--)$j % 2 == 0 prints *, else prints $iis_numeric(trim($line)) before using $necho PHP_EOL inside the inner loop$i = $n (repeats peak row)$j <= $i * 2 instead of $j < $i * 2$n = 1 edge casePrint the pattern the beginner-friendly way.
j%2: * or i
DefinitionTop + mirror
Code2*i-1 chars
Code$i = $n-1
ShapeO(n²) time
AnalysisThis pattern prints a top half (1..$n) and a bottom half ($n-1..1). Each row prints 2*$i-1 characters, alternating the row number and * using $j % 2.
Move on to the increasing number triangle starting from 11 in the PHP number-pattern series.
12 people found this page helpful