PHP Odd Number Triangle Pattern (Left-Shifted)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

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.

MethodIdeaBest for
Odd + += 2Both loops use += 2 from an odd startLearning, interviews, exams
Even mirrorStart both loops at 2Comparing 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

GoalPattern
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 rowecho PHP_EOL;
Force odd maxif ($max % 2 === 0) $max -= 1;
Even mirrorfor ($i = 2; $i <= $max; $i += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $jStays on the same lineEach odd digit
echo PHP_EOLEnds the current lineAfter the inner loop

Print all digits without a newline, then end the row once.

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 result max = 10 · 15 digits
13579
3579
579
79
9

Worked Walkthrough

Trace three outer values when $max = 10 — watch the start shift while only odds print.

$iInner $jLengthPrinted row
11, 3, 5, 7, 9513579
33, 5, 7, 943579
55, 7, 93579
9919

10 never prints because the loops stay on odd values. Raising $i by 2 drops one odd digit from the left each row.

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.

PHP
<?php
$max = 10;

for ($i = 1; $i <= $max; $i += 2) {
    for ($j = $i; $j <= $max; $j += 2) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Outer picks the start. $i runs 1, 3, 5, 7, 9 — that value is where each row begins.

2. Inner prints odds to max. $j steps from $i to $max by 2 with echo $j.

3. End the row. Call echo PHP_EOL; only after the inner loop finishes.

Example 2 — User Input

Read the maximum from the CLI. Subtract 1 when the value is even so the bound is odd.

PHP
<?php
echo "Enter the maximum value: ";
$max = (int) trim(fgets(STDIN));

if ($max % 2 === 0) {
    $max -= 1;
}

for ($i = 1; $i <= $max; $i += 2) {
    for ($j = $i; $j <= $max; $j += 2) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

1. Adjust even input. 8 becomes 7 so the last odd matches a clear odd bound.

2. Same loop core. The nested += 2 loops match Example 1.

3. Safer input tip. Validate before adjusting:

Safer input
echo "Enter the maximum value: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$max = (int) $input;
if ($max % 2 === 0) {
    $max -= 1;
}

Example 3 — Even Number Mirror

Start both loops at 2 to print only even digits with the same left-shift shape.

PHP
<?php
$max = 10;

for ($i = 2; $i <= $max; $i += 2) {
    for ($j = $i; $j <= $max; $j += 2) {
        echo $j;
    }
    echo PHP_EOL;
}
?>

How It Works

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.

Time and Space Complexity

ProgramTimeExtra 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²).

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.

Next: Alternating Odd/Even Triangle

Continue with rows that flip between odd and even number sequences.

Program 18 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful