A left-shifted odd number triangle prints only odd values from the row start up to $max, stepping by 2 — so you see 13579, 3579, 579, 79, 9.
Remember
Rule: for i = 1..max step 2, print j = i..max step 2
13579
3579
579
79
9 ← max = 10
In PHP both loops use += 2 from an odd start: echo $j prints each odd digit, then echo PHP_EOL ends the row. Program 18 alternates odd and even rows next.
Approach
How to Solve It
Outer loop from 1 to $max stepping by 2. Inner loop from current $i to $max stepping by 2. End each row with echo PHP_EOL.
Method
Idea
Best for
Odd + += 2
Both loops use += 2 from an odd start
Learning, interviews, exams
Even mirror
Start both loops at 2
Comparing odd-only vs even-only
Pseudocode
Pseudocode
for i from 1 to max step 2:
for j from i to max step 2:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Outer (odd starts)
for ($i = 1; $i <= $max; $i += 2)
Inner (odds to max)
for ($j = $i; $j <= $max; $j += 2) echo $j;
End the row
echo PHP_EOL;
Force odd max
if ($max % 2 === 0) $max -= 1;
Even mirror
for ($i = 2; $i <= $max; $i += 2)
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j
Stays on the same line
Each odd digit
echo PHP_EOL
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the maximum and the left-shifted odd triangle updates instantly.
Whole numbers from 1 to 20. Odd steps never print even values — tap a chip or type a value.
Live resultmax = 10 · 15 digits
13579
3579
579
79
9
Trace
Worked Walkthrough
Trace three outer values when $max = 10 — watch the start shift while only odds print.
$i
Inner $j
Length
Printed row
1
1, 3, 5, 7, 9
5
13579
3
3, 5, 7, 9
4
3579
5
5, 7, 9
3
579
9
9
1
9
10 never prints because the loops stay on odd values. Raising $i by 2 drops one odd digit from the left each row.
Code
PHP Programs
Three complete programs: fixed max, CLI input with odd adjust, and an even-number mirror. Use View Output for sample results.
Example 1 — Fixed $max = 10
Hard-coded maximum — both loops step by 2 from an odd start.
1. Same left-shift structure. Only the start value changes from 1 to 2.
2. Even values only.$i and $j visit 2, 4, 6, 8, 10.
3. Compare side by side. Seeing both odd and even mirrors makes the += 2 step easy to remember.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong step
Use $j += 2, not $j++
Using $j++ reprints consecutive integers, not an odd-only triangle.
even max
Optional: force an odd bound
if ($max % 2 === 0) $max -= 1; keeps the last odd clear when reading input.
PHP_EOL inside
echo PHP_EOL inside the inner loop
That puts each digit on its own line. Print digits without a newline; call echo PHP_EOL only after the row finishes.
max = 1
Single digit
Output is just 1 — a good sanity check.
max ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
$max > 9
echo 11 prints two characters. Labs often stick to max ≤ 10 for a clean single-digit look.
Analysis
Time and Space Complexity
Program
Time
Extra space
Odd triangle (Examples 1–2)
O(n²)
O(1)
Even mirror (Example 3)
O(n²)
O(1)
About half the integers up to n are visited, but nested loops still make total work O(n²).
Remember
Key Takeaways
Rule: for each odd start $i, print odds from $i to $max with step 2.
Shift: each row starts at a larger odd value, so the left edge moves right.
echo vs PHP_EOL: digits stay on the line; echo PHP_EOL advances after each row.
Next step: Program 18 alternates odd and even number rows.
One line: start at an odd $i, print odds through $max with += 2, then raise the start.
Frequently Asked Questions
Both loops increment by 2 ($i += 2 and $j += 2), so they visit only odd values: 1, 3, 5, 7, 9.
Each next row starts from a larger odd number ($i increases by 2), so fewer digits print on that row.
On the last iteration, $i = 9, so the inner loop prints only 9 once.
echo $j stays on the same line. echo PHP_EOL ends the current line. Digits use echo; the row break uses echo PHP_EOL after the inner loop.
Subtract 1 ($max -= 1) so the last printed odd matches the intended width, as shown in Example 2.
Yes. Start both loops at 2 instead of 1 — see Example 3 for the even mirror.
O(n²) for maximum value n. Only about half the numbers are visited due to step size 2, but nested loops still dominate.
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?
Both loops step by 2 with $i += 2 and $j += 2, so only odd numbers print. Each row starts at a larger odd value, so the triangle shifts left — still O(n²) for maximum n.