Shifting Odd Number Pattern in PHP

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Step Size Loops

What You’ll Learn

How to print a shifting odd-number pattern in PHP where each next row starts from the next odd number: 13579, 3579, 579, 79, 9.

You’ll practice nested loops with a step size of 2 to iterate through odd numbers only.

⭐ Pattern Output

For odd numbers up to 9, the pattern looks like this:

Output
13579
3579
579
79
9
1

Complete PHP Program

The outer loop picks the starting odd number. The inner loop prints odd numbers from that start up to the maximum odd value.

PHP
<?php
$maxOdd = 9;

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

🧠 How It Works

1

Choose the max odd number

$maxOdd = 9; sets the last number printed in each row.

Setup
2

Outer loop (starting odd)

for ($i = 1; $i <= $maxOdd; $i += 2) picks the first number of each row: 1, 3, 5, 7, 9.

Row start
3

Inner loop (print remaining odds)

for ($j = $i; $j <= $maxOdd; $j += 2) prints odd numbers from the current start up to $maxOdd.

Printing
4

New line per row

echo PHP_EOL; moves to the next line after each row.

Line break
=

Shifted odd-number rows

Each row starts later, so fewer numbers are printed: 5 digits, then 4, 3, 2, and 1.

2

Variation — User Input (CLI) Version

Let the user choose the maximum odd number at runtime (run from terminal with php):

PHP
<?php
echo "Enter the maximum odd number (e.g., 9): ";
$maxOdd = (int) trim(fgets(STDIN));

if ($maxOdd < 1) {
    echo "Please enter a positive number." . PHP_EOL;
    exit(1);
}

if ($maxOdd % 2 === 0) {
    $maxOdd -= 1; // adjust to the nearest lower odd number
}

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

💡 Tips for Enhancement

Try These

  • Print spaces between numbers for readability on larger max values
  • Start from a different first odd (like 3) to shift the triangle
  • Reverse the pattern by printing from the max odd down to the start
  • Replace odds with even numbers by starting at 2 and stepping by 2
  • Validate CLI input and auto-adjust even numbers to odd

Avoid

  • Using a max value of 0 or negative without validation
  • Forgetting the newline after each row
  • Mixing step sizes (both loops should step by 2 for odds)
  • Assuming user input is already odd in CLI mode

Key Takeaways

1

Use step size 2 to iterate only over odd numbers.

2

The outer loop selects the start of each row.

3

The inner loop prints from the start odd up to the max odd.

4

Auto-adjusting even input to odd makes the CLI version more user-friendly.

❓ Frequently Asked Questions

Both loops increment by 2, so they visit only odd values: 1, 3, 5, 7, 9.
Set $maxOdd = 15 (or read it from input). Keep += 2 in both loops.
Yes—start at 2 and step by 2 (2, 4, 6, ...). Also set the max to an even number.
O(n²) relative to the max width n. Each row prints fewer elements, but total work grows quadratically with the width.

Explore More PHP Number Patterns!

Practice step-size loops and shifting starts—they show up in many number and matrix problems.

All Number Patterns →
Did you know?

Loop step sizes (like += 2) are a simple way to iterate through filtered sequences—odd numbers, even numbers, or every k-th element.

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