Home PHP Number Patterns Program 46 PHP Concentric Number Square Pattern Definition
What Is This Pattern? A concentric number square prints layers from $k down to 1: outer values stay large, the center is 1, and each row mirrors left-to-right.
Rule: print max(i, j) style via (j > i ? j : i); left k..1, right 2..k
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5 ← k = 5 In PHP outer loop $i from $k down to 1. Per row: left half $j = $k..1, right half $j = 2..$k, value $j > $i ? $j : $i.
Approach
How to Solve It One outer layer loop, then left and right halves with the same cell rule.
Method Idea Best for Two-half row Left $k..1, right 2..$k, value $j > $i ? $j : $i Learning, interviews, exams Input $k Same logic with a user-chosen peak Practice / demos
Pseudocode for i from k down to 1:
for j from k down to 1:
print (j if j > i else i) and a space
for j from 2 to k:
print (j if j > i else i) and a space
print newline Cheat sheet Goal Pattern Layer loop for ($i = $k; $i >= 1; $i--)Left half for ($j = $k; $j >= 1; $j--)Right half for ($j = 2; $j <= $k; $j++)Cell value echo ($j > $i ? $j : $i) . " ";End the row echo PHP_EOL;
Printing Numbers vs Starting a New Line API Effect Use for echo ($j > $i ? $j : $i) . " "Stays on the same line Each number plus a trailing space echo PHP_EOLEnds the current line After both half-loops finish a row
Print numbers without a newline, then end the row once.
Try it
Live Preview Change $k and the concentric square updates instantly.
Trace
Worked Walkthrough — $k = 3 Trace each layer: left half, then mirrored right half.
$iLeft $j=3..1 Right $j=2..3 Printed row 33 3 33 33 3 3 3 323 2 22 33 2 2 2 313 2 12 33 2 1 2 3
Rows = $k, columns = 2*$k - 1. Total cells for $k = 5 = 5 × 9 = 45.
Code
PHP Programs Three complete programs: fixed $k = 5, user-input $k, and a compact $k = 3 demo. Use View Output for sample results.
1. Fixed k 2. User input 3. Compact demo Example 1 — Fixed $k = 5 If-else cell rule with explicit left and right half-loops.
<?php
$k = 5;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
if ($j > $i)
echo $j . " ";
else
echo $i . " ";
}
for ($j = 2; $j <= $k; $j++) {
if ($j > $i)
echo $j . " ";
else
echo $i . " ";
}
echo PHP_EOL;
} 5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5 How It Works 1. Layers. $i runs from 5 down to 1 — each value is one concentric row.
2. Left half. $j runs $k..1; print $j when $j > $i, else print $i.
3. Right half. $j runs 2..$k with the same rule so the row mirrors without doubling the center.
Example 2 — User Input ($k) Read $k and use a ternary for the cell value.
<?php
echo "Enter k: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
echo "Please enter a positive whole number." . PHP_EOL;
exit(1);
}
$k = (int) $input;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--)
echo ($j > $i ? $j : $i) . " ";
for ($j = 2; $j <= $k; $j++)
echo ($j > $i ? $j : $i) . " ";
echo PHP_EOL;
} How It Works 1. Validate k. Check is_numeric; require $k >= 1.
2. Same square. Only the peak value changes; width becomes 2*$k - 1.
Example 3 — Compact $k = 3 A smaller fixed demo — easy to trace before scaling to 5.
<?php
$k = 3;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--)
echo ($j > $i ? $j : $i) . " ";
for ($j = 2; $j <= $k; $j++)
echo ($j > $i ? $j : $i) . " ";
echo PHP_EOL;
} 3 3 3 3 3
3 2 2 2 3
3 2 1 2 3 How It Works 1. Same rules. Layer down; left then right; ternary picks $j or $i.
2. Quick check. Three rows, five columns; center of the last row is 1.
Edge Cases & Pitfalls Check these before calling the solution done.
j = 1..k Right half starts at $j = 1 The center digit prints twice. Start the mirror loop at $j = 2.
i++ Outer loop goes 1..$k You get the inverted order (center first). Keep $i from $k down to 1.
j < i Flip the comparison Using $j < $i inverts layers. Stay with $j > $i ? $j : $i.
k = 1 Single cell Output is just 1 — the right half loop never runs.
PHP_EOL echo PHP_EOL inside a half-loopThat breaks the row. Call echo PHP_EOL only after both inner loops.
Bad input Casting without checks 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($k²)O(1)User input (Example 2) O($k²)O(1)
There are $k rows and 2*$k - 1 cells per row, so total prints are about 2*$k² — quadratic in $k.
Remember
Key Takeaways Rule: layers $k..1; cell value is $j > $i ? $j : $i.
Shape: left $k..1, right 2..$k — width 2*$k - 1.
echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after both halves.
Next step: Program 47 extends this into a full concentric number diamond.
One line: for each layer, print a mirrored row using $j > $i ? $j : $i.
Frequently Asked Questions What does this pattern print? A concentric number square: the outer layer is k (e.g. 5), values decrease toward the center to 1, then mirror back out on each row.
Why is the output symmetric? Each row prints a left half ($j = $k..1) and a right half ($j = 2..$k) with the same $j > $i rule, mirroring around the center.
What does j > i mean? When column $j is still outside the current row layer $i, print $j. Otherwise print $i (the current layer value).
Why two inner loops per row? The first builds the left descending half; the second mirrors columns 2..k on the right without repeating the center digit.
How can I change the maximum number? Change $k (or read it from input). Total width becomes 2*$k - 1 — see Example 2.
What is the time complexity? O(k²) because each of k rows prints about 2k - 1 cells.
How is this different from Program 45? Program 45 prints a star-and-zero X on a fixed grid. Program 46 prints decreasing/increasing numbers in a concentric square.
What is the grid width? Each row has 2*$k - 1 numbers. For $k = 5, width is 9 columns.
How should I read k safely? Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know? Each cell prints $j when $j > $i, else $i. Row $i runs from $k down to 1; grid width = 2k - 1 columns per row.
Next: Concentric Number Diamond Mirror this square top and bottom into a full diamond (5..1..5).
Program 47 tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful
Helpful Share Copy link Suggestion