Home PHP Number Patterns Program 41 PHP Square Number Pyramid Pattern Definition
What Is This Pattern? A square-numbers pyramid prints consecutive perfect squares in centered rows whose widths are odd: 1, then 3, then 5, and so on.
Rule: odd row widths; print next m² with width 4
1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625 ← 5 levels In PHP use $maxWidth = 2 * $levels - 1. The outer loop steps $i by 2; indent with spaces, then print $i squares from a running counter $m.
Approach
How to Solve It One odd-step outer loop with indent, then a continuous square counter.
Method Idea Best for Centered pyramid Indent, then print $m*$m with %4d Learning, interviews, exams Levels input Same logic with $maxWidth = 2*$levels - 1 Practice / demos
Pseudocode maxWidth = 2 * levels - 1
m = 1
for i from 1 to maxWidth step 2:
for j from i to maxWidth-1:
print two spaces
repeat i times:
print m*m (width 4), then m = m + 1
print newline Cheat sheet Goal Pattern Odd row widths for ($i = 1; $i <= $maxWidth; $i += 2)Center the row for ($j = $i; $j < $maxWidth; $j++) echo " ";Next square printf("%4d", $m * $m); $m++;End the row echo PHP_EOL;
Printing Numbers vs Starting a New Line API Effect Use for echo " " / printf("%4d", ...)Stays on the same line Each indent or square echo PHP_EOLEnds the current line After both inner loops
Print indents and squares without a newline, then end the row once.
Try it
Live Preview Change the level count and the square pyramid updates instantly.
Trace
Worked Walkthrough — $levels = 3 $maxWidth = 5. Trace indents and the running square counter.
$iIndent / squares Printed row 14 pads / 1 132 pads / 4 9 16 4 9 165none / 25..81 25 36 49 64 81
Total squares for n levels = n². Counter $m never resets between rows.
Code
PHP Programs Three complete programs: fixed 5 levels, CLI input levels, and a compact 3-level demo. Use View Output for sample results.
1. Fixed levels 2. User input 3. Compact demo Example 1 — Fixed $levels = 5 ($maxWidth = 9) Odd-step outer loop: indent, then print consecutive $m*$m values with width 4.
<?php
$m = 1;
for ($i = 1; $i <= 9; $i += 2) {
for ($j = $i; $j < 9; $j++)
echo " ";
for ($k = 1; $k <= $i; $k++) {
printf("%4d", $m * $m);
$m++;
}
echo PHP_EOL;
} 1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625 How It Works 1. Odd widths. $i takes 1, 3, 5, 7, 9 — that many squares print on each row.
2. Indent. Print " " while $j runs from $i to just below 9 so narrow rows stay centered.
3. Squares. Print $m*$m with printf("%4d", ...), then increment $m — never reset between rows.
Example 2 — User Input (levels) Read the level count from STDIN and set $maxWidth = 2 * $levels - 1.
<?php
echo "Enter levels: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
echo "Please enter a positive whole number." . PHP_EOL;
exit(1);
}
$levels = (int) $input;
$maxWidth = 2 * $levels - 1;
$m = 1;
for ($i = 1; $i <= $maxWidth; $i += 2) {
for ($j = $i; $j < $maxWidth; $j++)
echo " ";
for ($k = 1; $k <= $i; $k++) {
printf("%4d", $m * $m);
$m++;
}
echo PHP_EOL;
} How It Works 1. Validate levels. Check is_numeric; require $levels >= 1.
2. Same pyramid. Three levels end at 81 with nine squares total.
Example 3 — Compact $levels = 3 A smaller fixed demo — same centered idea, easier to trace by hand.
<?php
$levels = 3;
$maxWidth = 2 * $levels - 1;
$m = 1;
for ($i = 1; $i <= $maxWidth; $i += 2) {
for ($j = $i; $j < $maxWidth; $j++)
echo " ";
for ($k = 1; $k <= $i; $k++) {
printf("%4d", $m * $m);
$m++;
}
echo PHP_EOL;
} How It Works 1. Same rules. Odd $i sets count; indent; print the next squares.
2. Quick check. The base row has five values: 25 through 81.
Edge Cases & Pitfalls Check these before calling the solution done.
reset $m Reset $m = 1 each row That reprints 1, 4, 9 on every line. Keep $m outside the outer loop.
$i++ Use $i++ instead of $i += 2 Row widths become 1, 2, 3… instead of odd lengths. Keep the step of 2.
no format Print $m*$m without width Columns drift once values hit three digits. Prefer printf("%4d", $m * $m).
levels = 1 Single 1 Output is just the formatted 1 — one level, one square.
levels ≤ 0 Empty output The outer loop never runs. Validate and prompt again for clearer UX.
Bad input (int) alone is not enoughPrefer 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)
Total printed squares are 1 + 3 + 5 + … + (2n-1) = n², so work is quadratic in the level count.
Remember
Key Takeaways Rule: odd row widths via $i += 2; each cell prints the next $m*$m.
Center: indent with " " before printing; use %4d for column alignment.
echo vs PHP_EOL: indents and squares stay on the line; echo PHP_EOL advances after each row.
Next step: Program 42 prints a hollow square border of 1s.
One line: for each odd width, indent then print the next perfect squares from a shared counter.
Frequently Asked Questions What does this pattern print? A centered pyramid of perfect squares: row 1 prints 1 (1²), row 2 prints 4 9 16 (2², 3², 4²), and so on.
Why do row sizes go 1, 3, 5, 7, 9? The outer loop increases $i by 2 each time ($i += 2), so $i takes odd values — each becomes the count of squares printed on that row.
How is the pyramid centered? An indentation loop prints spaces before each row. As $i grows, fewer spaces are printed, so wider rows shift left and stay centered.
What does counter $m do? $m starts at 1 and increments after every printed square. Each value printed is $m*$m — the next perfect square in sequence.
Why use %4d formatting? Fixed-width columns keep the pyramid aligned as squares grow from 1 to 625. Without it, columns drift apart.
How can I make the pyramid larger? Increase levels so $maxWidth = 2*$levels - 1 grows — see Example 2.
How is this different from Program 40? Program 40 alternates 1 and 0 with shrinking rows. Program 41 prints perfect squares in a centered pyramid with growing odd-width rows.
What is the time complexity? O(n²) for n levels — total prints are 1+3+5+...+(2n-1) = n².
How should I read levels safely? Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know? Each printed value is $m² from a running counter $m. Row widths are odd (1, 3, 5, 7, 9) — total prints for n levels = n².
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