Shape Rule
Alternating 1/0, shrinking width
Odd rows print 1, even rows print 0; each row is one digit repeated.

The alternating 1/0 pattern repeats one digit per row while the width shrinks. Odd rows print 1, even rows print 0. This tutorial covers the parity rule, nested loops, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Alternating 1/0, shrinking width
Odd rows print 1, even rows print 0; each row is one digit repeated.
$i % 2
if ($i % 2 == 0) prints 0; otherwise print 1 on every inner iteration.
$j = $i..$rows
for ($j = $i; $j <= $rows; $j++) controls width — $rows - $i + 1 repeats per row.
Row index i
for ($i = 1; $i <= $rows; $i++) walks each row and sets parity.
1–20 rows
Pick a row count and draw the alternating 1/0 pattern instantly in the browser.
Complexity
Total digits = n(n+1)/2; extra memory stays O(1).
An alternating 1/0 pattern repeats one digit per row while the line gets shorter. With $rows = 5, the output is 11111, 0000, 111, 00, and 1.
In PHP you use one outer loop for row parity ($i % 2) and one inner loop from $j = $i to $rows to control width, then call echo PHP_EOL after each row.
It combines a simple parity check with shrinking inner-loop bounds — a common interview building block.
Odd rows: 1; even rows: 0 via $i % 2.
Each row prints $rows - $i + 1 copies of the chosen digit.
echo the digit in the inner loop; echo PHP_EOL after.
Follow Program 39 rotating pattern; continue to Program 41 square pyramid.
In short: for each row $i from 1 to $rows, print 1 or 0 based on $i % 2, repeat $rows - $i + 1 times, then call echo PHP_EOL.
Given a positive integer $rows (e.g. 5), print an alternating 1/0 triangle where odd rows repeat 1 and even rows repeat 0, with width $rows - $i + 1 on row $i.
// $rows = 5
//11111
//0000
//111
//00
//1 | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Left-aligned rows of repeated 1 or 0; row $i has $rows - $i + 1 characters. |
for i from 1 to rows:
pick digit = 1 if i is odd else 0
for j from i to rows:
print digit
print newline | Approach | Idea | Best for |
|---|---|---|
| Parity + nested loops | 11111, 0000, … | Learning and interviews |
| User-input rows | trim(fgets(STDIN)); | Flexible console programs |
| Spaced output | echo ($i % 2 == 0 ? "0" : "1") . " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk each row | for ($i = 1; $i <= $rows; $i++) |
| Pick digit by parity | if ($i % 2 == 0) echo "0"; else echo "1"; |
| Control row width | for ($j = $i; $j <= $rows; $j++) |
| End the row | echo PHP_EOL; |
| Program 39 contrast | Rotation uses two inner loops; this pattern uses parity + one inner loop |
Same alternating row — how parity and the inner loop work together.
$i % 2Odd rows print 1; even rows print 0
$j = $i..$rowsRepeats the digit $rows - $i + 1 times
shrinksRow $i has exactly $rows - $i + 1 characters
trace i=2Dry-run row 2: even parity, inner loop 2..5 → four zeros
Reach for this pattern when teaching parity check and one inner loop on the same row.
Most PHP pattern series start here before pyramids and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 39 (rotating), then continue to Program 41 (square pyramid).
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 row count between 1 and 20 and draw the alternating 1/0 pattern in the browser.
Three complete PHP programs — fixed row count, fgets(STDIN) input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows with parity check and nested loops per line.
$rows = 5Hard-coded size — parity check and one inner loop build each alternating row.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++) {
if ($i % 2 == 0) {
echo "0";
} else {
echo "1";
}
}
echo PHP_EOL;
} When $i = 1, the inner loop runs 5 times and prints 1 each time — output 11111. When $i = 2, it runs 4 times with even parity — output 0000.
Let the user choose the height at runtime.
Read the row count with trim(fgets(STDIN)) (check is_numeric() in real apps).
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++) {
echo ($i % 2 == 0 ? "0" : "1");
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $rows changes. Non-numeric input fails is_numeric() — validate before casting to int for safer labs.
Same alternating rows with spaces between digits for easier reading.
Append a space after each repeated digit for easier reading.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= $rows; $j++) {
echo ($i % 2 == 0 ? "0" : "1") . " ";
}
echo PHP_EOL;
} Same loop structure; only the print calls add + " " after each repeated digit.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j.
for ($i = 1; $i <= $rows; $i++) walks each row and sets parity via $i % 2.
for ($j = $i; $j <= $rows; $j++) repeats the chosen digit $rows - $i + 1 times.
If $i % 2 == 0 print 0; else print 1, then echo PHP_EOL ends the row.
Total digit prints: n(n+1)/2 — O(n²) time, O(1) extra memory.
$rows = 5Trace each row: parity pick, inner-loop range, width, and full row output.
$i | Parity | Inner loop (j) | Width | Row output |
|---|---|---|---|---|
1 | odd | 1..5 | 5 | 11111 |
2 | even | 2..5 | 4 | 0000 |
3 | odd | 3..5 | 3 | 111 |
4 | even | 4..5 | 2 | 00 |
5 | odd | 5..5 | 1 | 1 |
Total character prints: 5+4+3+2+1 = 15 = n(n+1)/2.
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: change k start to 100 for a shifted sequence.
Practice echo vs echo 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: use %4d when values exceed two digits.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject $rows <= 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 PHP 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 the parity + width version first; then try spaced output for a grid-like view.
Small habits that keep number-pattern code clean.
Use $rows (or $n) and keep $i/$j for loops — or rename to $row/$col.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
echo ($i % 2 == 0 ? "0" : "1"); keeps the inner loop compact.
Trace $rows = 3 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 alternating 1/0 patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo for the digit; echo PHP_EOL only after the inner loop.
for ($j = 1; $j <= $i; $j++) grows width instead of shrinking — you get a different triangle shape.
→ Keep for ($j = $i; $j <= $rows; $j++) so each row shortens by one character.
Omitting echo PHP_EOL glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave $rows uninitialized.
→ Prefer fgets(STDIN) and re-prompt on failure.
Checking $j % 2 instead of $i % 2 alternates digits within a row instead of between rows.
→ Base parity on the outer index $i, not the inner counter $j.
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.
$rows < 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 $rows unset — call is_numeric($input) first.
Swap the if/else branches to start with 0 on row 1.
Try these variations to lock in the pattern.
0 on odd rows instead of 1n(n+1)/2 — row $i contributes $rows - $i + 1 characters.print stays on the line; println advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 should print a single 1.1; even rows print 0 — flip the branches to reverse the start digit.Quick Takeaway: outer loop sets parity with $i % 2, inner loop $j = $i..$rows repeats the digit, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced output (Example 3) | O(rows²) | O(1) |
The alternating 1/0 pattern combines one outer loop with parity check and one inner loop per row — a natural step after rotating patterns. Master the compact digit output first, then optionally add spaces for readability.
Practice the three examples above, then continue to Program 41 for the square number pyramid.
Row $i prints one digit repeated $rows - $i + 1 times — keep echo PHP_EOL only after the inner loop finishes.
$i % 2) and inner bounds before codingecho in the inner loop and echo PHP_EOL after each row$rows ≥ 1 for interactive programsis_numeric($input) before using $rowsecho PHP_EOL inside the inner digit loop$j instead of $i (alternates within a row)$j = 1..$i when you meant shrinking width$rows = 1 edge casePrint the pattern the beginner-friendly way.
Odd row: 1; even row: 0
DefinitionSets row parity
Code$j = $i..$rows width
$rows - $i + 1 chars
O(n²) time
AnalysisEach row prints the same digit repeatedly — 1 on odd rows and 0 on even rows. The inner loop runs from $j = $i to $rows, so width is $rows - $i + 1 and shrinks each line.
Move on to the square number pyramid in the PHP number-pattern series.
12 people found this page helpful