An alternating 1/0 pattern repeats one digit per row while the line gets shorter — odd rows print 1, even rows print 0.
Remember
Rule: for i from 1 to rows,
print (i % 2 == 0 ? 0 : 1) repeated (rows - i + 1) times
11111
0000
111
00
1 ← rows = 5
Unlike Program 39 (rotating digits), here each row uses a single digit chosen by row parity.
Approach
How to Solve It
Walk i from 1 to rows; pick 1 or 0 from i % 2, then repeat it rows − i + 1 times.
Method
Idea
Best for
Parity + nested loops
i % 2 picks the digit; inner loop sets width
Learning, interviews, exams
Spaced digits
Same loops; print each digit with a trailing space
Readable demos and classroom slides
Pseudocode
Pseudocode
for i from 1 to rows:
digit = (i % 2 == 0) ? 0 : 1
for j from i to rows:
print digit
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Pick digit
i % 2 == 0 ? "0" : "1"
Shrinking width
for (int j = i; j <= rows; j++)
Print digit
System.out.print(digit);
End the row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each repeated digit
System.out.println
Ends the current line
After the inner loop finishes
Print values without a newline, then end the row once.
Try it
Live Preview
Change the row count and the alternating 1/0 triangle updates instantly.
Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
11111
0000
111
00
1
Trace
Worked Walkthrough — rows = 5
Trace the digit from i % 2 and the width rows − i + 1 for each row.
i
Parity
Digit
Width
Printed row
1
odd
1
5
11111
2
even
0
4
0000
3
odd
1
3
111
4
even
0
2
00
5
odd
1
1
1
Total digits = 5 + 4 + 3 + 2 + 1 = 15 — the triangular number n(n+1)/2.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — parity check and one inner loop build each row.
Java
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();
}
}
}
Output
11111
0000
111
00
1
How It Works
1. Outer loop.i is both the row index and the parity source.
2. Pick the digit. Even i → 0; odd i → 1.
3. Inner loop.j runs from i to rows — width shrinks each row.
When i = 1: five 1s → 11111. When i = 2: four 0s → 0000.
Example 2 — Rows Input
Read rows at runtime. Same parity rule with a compact ternary.
Java
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();
if (rows < 1) return;
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();
}
}
Output (when user enters 5)
Enter the number of rows: 5
11111
0000
111
00
1
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same core. Only the source of rows changes; the ternary replaces the if/else.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Spaced Output
Same nested loops — each digit printed with a trailing space for readability.
Java
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();
}
}
}
Output
1 1 1 1 1
0 0 0 0
1 1 1
0 0
1
How It Works
1. Same structure. Parity and width unchanged — only the print adds a space.
2. Why space? Digits are easier to count in demos and slides.
3. Flip tip. Swap "0" and "1" in the ternary to start with zeros.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of digits
If println is inside the inner loop, each digit lands on its own line. Use print for digits; println only after the inner loop.
Wrong parity
Starts with 0
Checking i % 2 != 0 for zero (or swapping the branches) flips which digit appears on odd rows.
Wrong width
Rectangle
Using j = 1..rows on every row prints a full rectangle instead of a shrinking triangle.
No println
One long line
Forgetting println() after the row glues every digit onto one endless line.
rows = 1
Single digit
Output is just 1 on one line.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Alternating 1/0 (Examples 1–3)
O(n²)
O(1)
Total digits = n(n+1)/2 — still quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: odd rows repeat 1; even rows repeat 0; width is rows − i + 1.
Parity: use i % 2 (or a ternary) to choose the digit once per row.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, print (i % 2 == 0 ? 0 : 1) repeated rows − i + 1 times, then println().
Frequently Asked Questions
The code checks i % 2. When i is even it prints 0; otherwise it prints 1.
The inner loop runs from j = i to rows, which is rows - i + 1 iterations — one fewer character each row.
Yes. Swap the digits in the if/else, or flip the parity check to print 0 on odd rows and 1 on even rows.
Program 39 rotates digits 12345, 23451, etc. Program 40 repeats a single digit per row and alternates 1/0 based on row parity.
Yes. Print "1 " and "0 " in the inner loop — see Example 3.
O(n²) for n rows because total printed characters are n + (n-1) + … + 1 = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Each 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.