Shape Rule
Odd numbers only
Row 1 prints 13579, row 2 prints 3579, row 3 prints 579, and so on as the start shifts right.

The left-shifted odd number triangle prints consecutive odd digits on each row while the starting odd value increases — a great exercise in nested loops with step size 2. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Odd numbers only
Row 1 prints 13579, row 2 prints 3579, row 3 prints 579, and so on as the start shifts right.
i += 2
for ($i = 1; $i <= $max; $i += 2) picks the starting odd number for each row: 1, 3, 5, 7, 9.
j += 2 to max
for ($j = $i; $j <= $max; $j += 2) prints odd digits from the row start up to $max.
Same line / next line
Odd digits use echo $j; end each row with echo PHP_EOL.
1–20 max
Pick a maximum value and draw the left-shifted odd triangle instantly in the browser.
Complexity
Total digit prints shrink each row; complexity is still O(n²) for maximum n.
A left-shifted odd number triangle prints consecutive odd numbers on each row while the starting odd value increases by 2. With max = 10, the output is 13579, 3579, 579, 79, 9.
In PHP you solve it with nested loops that step by 2: for ($i = 1; $i <= $max; $i += 2) and for ($j = $i; $j <= $max; $j += 2), then echo PHP_EOL; ends each row.
It teaches step-size loops (+= 2) before more complex parity-based patterns.
i += 2 and j += 2 visit only odd values.
Each row starts at a larger odd i, so fewer digits print.
echo $j in the inner loop; echo PHP_EOL after.
Follow Program 16; continue to Program 18 (alternating odd/even rows).
In short: for each odd start i from 1 to max, print odd j from i to max stepping by 2, then call echo PHP_EOL.
Given a positive integer max, print a left-shifted odd number triangle: row starting at odd i prints odd digits from i to max stepping by 2.
// max = 10 (conceptual shape)
// 13579
// 3579
// 579
// 79
// 9 | Item | Type | Description |
|---|---|---|
max | int | Upper bound for odd digits on each row (typically ≥ 1). |
| Printed output | text | Each row prints consecutive odd numbers from i to max. |
for i from 1 to max step 2:
for j from i to max step 2:
print j (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops + step 2 | 13579, 3579, … | Learning and interviews |
| Even max adjustment | if ($max % 2 === 0) $max -= 1; | User-input programs |
| Even-number variant | Start at 2 with += 2 | Mirror pattern with evens |
| Goal | Pattern |
|---|---|
| Walk each row start | for ($i = 1; $i <= $max; $i += 2) |
| Print odd digit | for ($j = $i; $j <= $max; $j += 2) echo $j; |
| End the row | echo PHP_EOL; |
| Force odd maximum | if ($max % 2 === 0) $max -= 1; |
| Even variant | for ($i = 2; $i <= $max; $i += 2) with $j = $i to $max |
| Spaced output | echo $j; |
Same left-shift idea — different bounds and step handling.
odd startOuter loop picks 1, 3, 5, 7, 9
odd digitsInner loop prints only odd values
even fixAdjust even user input to odd bound
trace i,jTrace max = 10 on paper before coding
Reach for this pattern when teaching loop step sizes and shrinking row widths.
Step-size loops build on binary patterns from Programs 15 and 16.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 16 (binary triangle) and Program 18 (alternating odd/even rows) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a maximum between 1 and 20 and draw the left-shifted odd triangle in the browser.
Three complete PHP programs — fixed maximum, user input, and an even-number mirror variant. Click View Output to reveal sample console results.
Print the left-shifted odd triangle with max = 10 and += 2 loops.
max = 10Hard-coded upper bound — ideal for first demos and screenshots.
<?php
for ($i = 1; $i <= 10; $i += 2) {
for ($j = $i; $j <= 10; $j += 2) {
echo $j;
}
echo PHP_EOL;
} When i = 1, the inner loop prints 1, 3, 5, 7, 9 as 13579. When i = 5, it prints 5, 7, 9 as 579, and so on as the start shifts right. echo PHP_EOL after the inner loop starts the next row.
Read the maximum with fgets(STDIN) and adjust even input to an odd bound.
Read max with (int) trim(fgets(STDIN)); subtract 1 when the value is even.
<?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;
} max = 8 becomes 7 after the even adjustment, so the last odd printed is 7. The nested += 2 loops stay the same as Example 1. Non-numeric input leaves max unset if you skip is_numeric() checks — always check it in safer labs.
Mirror the pattern with even numbers starting at 2 instead of 1.
Start both loops at 2 and step by 2 to print only even digits up to max = 10.
<?php
$max = 10;
for ($i = 2; $i <= $max; $i += 2) {
for ($j = $i; $j <= $max; $j += 2) {
echo $j;
}
echo PHP_EOL;
} Same left-shift structure as Example 1, but both loops start at 2 and visit only even values. Compare with the odd version to see how the start value changes the sequence.
Set $max = 10; and use fgets(STDIN) when reading input. Set max (fixed or from input).
for ($i = 1; $i <= $max; $i += 2) picks the starting odd number: 1, 3, 5, 7, 9.
for ($j = $i; $j <= $max; $j += 2) prints each odd value with echo $j.
echo PHP_EOL ends the row so the next outer iteration starts fresh.
Each row prints fewer digits as i grows — O(n²) time for maximum n, O(1) extra memory.
max = 10Trace each outer-loop value of i and note the odd j values printed on each row.
i | Inner j values | Printed row |
|---|---|---|
1 | 1, 3, 5, 7, 9 | 13579 |
3 | 3, 5, 7, 9 | 3579 |
5 | 5, 7, 9 | 579 |
7 | 7, 9 | 79 |
9 | 9 | 9 |
Five rows for max = 10; digit count shrinks from 5 down to 1.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice echo vs PHP_EOL without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with fgets(STDIN) and is_numeric() checks and positive-row checks.
Example: reject max <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn += 2 first; compare with j++ to see how step size changes which numbers print.
Small habits that keep number-pattern code clean.
Use max (or n) and keep i/j for row/column — or rename to start/value.
Call is_numeric(trim($line)) so bad input does not leave max uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
if ($max % 2 === 0) $max -= 1; keeps the bound odd when reading input.
Trace max = 10 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break left-shifted odd number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $j for digits; echo PHP_EOL only after the inner loop.
j++ prints even numbers too — the row no longer contains only odds.
→ For odd-only rows, keep for ($j = $i; $j <= $max; $j += 2).
Omitting echo PHP_EOL glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave max uninitialized.
→ Prefer is_numeric() and re-prompt on failure.
Switching to i = 0 without adjusting the inner bound prints an empty first row or wrong counts.
→ If 0-based, print i with wrong inner bound (e.g. j <= i + 1).
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
max < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves max unset — call is_numeric(trim($line)) first.
Subtract 1 or prompt again — otherwise the last odd may not match intent.
j++ includes even numbers — use j += 2 for odd-only output.
Try these variations to lock in the pattern.
j = 1 to i with j % 2+= 2 on both loopsecho $j between digitsi grows — still O(n²) prints for maximum n.echo $j stays on the line; echo PHP_EOL advances — mix them carefully.max > 0 for interactive programs; max = 1 should print a single 1.Quick Takeaway: outer loop steps by 2 for row starts, inner loop prints odd j up to max, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(max²) | O(1) |
| Even variant (Example 3) | O(max²) | O(1) |
The left-shifted odd number triangle is a compact lesson in loop step sizes: += 2 on both loops prints only odd values while each row starts later. Master the fixed-max version, then try the user-input and even-mirror variants.
Practice the three examples above, then continue to Program 18 for the alternating odd/even number triangle.
Use $i += 2 and $j += 2 for odd-only digits — keep echo for numbers and echo PHP_EOL; for the break, and adjust even $max when reading input.
i += 2 and j += 2 before codingecho $j for digits and echo PHP_EOL after each rowmax ≥ 1 for interactive programsis_numeric(trim($line)) before using maxecho PHP_EOL inside the inner digit loopj++ when you meant odd-only with j += 2max = 1 edge casePrint the pattern the beginner-friendly way.
Only odd digits print
Definitioni += 2 picks start
Codej += 2 to max
CodeRows shrink each line
ShapeO(n²) time
AnalysisBoth 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.
Move on to the alternating odd/even number triangle in the PHP number-pattern series.
12 people found this page helpful