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 Java 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 Java you use one outer loop for row parity (i % 2) and one inner loop from j = i to rows to control width, then call System.out.println() 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.
System.out.print the digit in the inner loop; println() 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 System.out.println().
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 (conceptual shape)
// 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 | sc.nextInt(); | Flexible console programs |
| Spaced output | System.out.print(digit + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk each row | for (i = 1; i <= rows; i++) |
| Pick digit by parity | if (i % 2 == 0) print "0"; else print "1"; |
| Control row width | for (j = i; j <= rows; j++) |
| End the row | System.out.println(); |
| 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 Java pattern series start here before pyramids and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner 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 Java programs — fixed row count, Scanner 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.
public class AlternatingOneZeroPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
if (i % 2 == 0) {
System.out.print("0");
} else {
System.out.print("1");
}
}
System.out.println();
}
}
} 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 Scanner.nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class AlternatingOneZeroPatternInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print(i % 2 == 0 ? "0" : "1");
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Same alternating rows with spaces between digits for easier reading.
Append a space after each repeated digit for easier reading.
public class AlternatingOneZeroSpaced {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print((i % 2 == 0 ? "0" : "1") + " ");
}
System.out.println();
}
}
} Same loop structure; only the print calls add + " " after each repeated digit.
System.out is built in; use Scanner when reading input. Set rows (fixed or from input).
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 println() 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 System.out.print vs row newline without complex math.
Example: put System.out.println() 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 Scanner 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 Java 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 row/column — or rename to row/col.
ScannerAvoid crashes when the user types letters instead of a number.
Only call System.out.println() after the inner loop finishes the row.
System.out.print(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 System.out.println() 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 System.out.print for the digit; System.out.println() 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 System.out.println() glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input throw undefined rows.
→ Prefer Scanner 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 Scanner leaves rows unset — call sc.hasNextInt() 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 println() only after the inner loop finishes.
i % 2) and inner bounds before codingSystem.out.print in the inner loop and println() after each rowrows ≥ 1 for interactive programsScanner return value before using rowsSystem.out.println() inside the inner digit loopj instead of i (alternates within a row)j = 1..i when you meant shrinking widthrows = 1 edge casePrint the pattern the beginner-friendly way.
Odd row: 1; even row: 0
DefinitionSets row parity
Codej = i..rows width
Logicrows - i + 1 chars
I/OO(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 Java number-pattern series.
12 people found this page helpful