The Binary 1-0-1 Triangle alternates 1 and 0 in a checkerboard pattern (1 / 0 1 / 1 0 1 / 0 1 0 1) using coordinate parity. With outer row index i and inner column index j both starting from 1, compute sum = i + j. Whenever sum % 2 == 0 (even coordinate sum), print 1; whenever sum % 2 != 0 (odd coordinate sum), print 0. This single modulus test handles row transitions cleanly.
- 1. Checkerboard Parity Using (i + j) Modulus 2
- 2. Coordinate Sum Dry Run for 5 Rows
- 3. Modulus Parity vs Toggle Variable Approaches
- 4. BinaryTriangle.java (BlueJ Ready)
- 5. Variable Description Table (VDT) for BinaryTriangle
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. Checkerboard Parity Using (i + j) Modulus 2
Looking at the Binary 1-0-1 Triangle row by row reveals two simultaneous rules: digits alternate horizontally across every row AND the starting digit alternates vertically down the first column (1, 0, 1, 0, 1). Checking only column parity j % 2 fails because every row's first column has j = 1, forcing every line to begin identically. Instead, examine the sum of the two loop coordinates, i + j. On the main diagonal or any cell where row and column share the same parity (both odd or both even), i + j is always even, so (i + j) % 2 == 0. Moving one step right increases j by 1 and flips parity to odd; moving one step down increases i by 1 and also flips parity to odd. One modulus test captures both horizontal and vertical alternation automatically.
- Odd + Odd = Even and Even + Even = Even, so matching parities yield sum % 2 == 0 and print 1.
- Odd + Even = Odd, so mismatched parities yield sum % 2 == 1 and print 0.
- Every single step right (+j) or down (+i) flips the parity bit cleanly.
2. Coordinate Sum Dry Run for 5 Rows
Trace the exact values of i, j, and sum for rows = 5. At i = 1, j = 1 gives sum = 2 (even), printing "1 ". At i = 2, j = 1 gives sum = 3 (odd -> "0 ") and j = 2 gives sum = 4 (even -> "1 "), producing "0 1 ". At i = 3, sums are 3+1=4, 3+2=5, 3+3=6, printing "1 0 1 ". At i = 4, sums are 4+1=5, 4+2=6, 4+3=7, 4+4=8, printing "0 1 0 1 ". Finally, at i = 5, j iterates 1 to 5 yielding sums 6, 7, 8, 9, 10—even, odd, even, odd, even—so row 5 outputs "1 0 1 0 1 ". Notice that the main diagonal cells (i == j) always satisfy 2 * i, which is strictly even and guarantees a 1 along the entire slanted hypotenuse.
- i = 1: sum = 2 -> prints 1
- i = 2: sums = 3, 4 -> prints 0 1
- i = 3: sums = 4, 5, 6 -> prints 1 0 1
- i = 4 and 5 continue alternating cleanly with every diagonal cell (i == j) equal to 1.
3. Modulus Parity vs Toggle Variable Approaches
Students sometimes attempt binary triangles by maintaining a toggle variable int bit = 1 and flipping it via bit = 1 - bit after every print. While that works for simple alternating patterns like 1 / 0 1 / 1 0 1, the toggle breaks down completely on even-length rows if a continuous global toggle isn't intended—for instance, after row 2 finishes with 1, a global toggle makes row 3 start with 0 instead of 1 unless you also re-initialize bit = i % 2 at the top of each row. By contrast, (i + j) % 2 is stateless across iterations: any cell's value depends strictly on its (i, j) coordinates. Furthermore, if an ICSE question asks for 1 / 1 0 / 1 0 1 (where every row begins with 1), you simply switch the condition from sum % 2 == 0 to j % 2 != 0.
- Coordinate parity (i + j) % 2 avoids stateful flag bugs across even and odd row lengths.
- If using a toggle bit, set bit = i % 2 before the inner loop and flip with bit = 1 - bit.
- Switching to j % 2 != 0 transforms the output into 1 / 1 0 / 1 0 1.
4. BinaryTriangle.java (BlueJ Ready)
import java.util.Scanner;
public class BinaryTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
int sum = i + j;
if (sum % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter number of rows: 5
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
5. Variable Description Table (VDT) for BinaryTriangle
| Variable Name | Data Type | Purpose in Program |
|---|---|---|
| sc | Scanner | Reads the number of rows entered by the user from standard input |
| rows | int | Stores the height of the binary triangle pattern |
| i | int | Outer loop index tracking the active row from 1 to rows |
| j | int | Inner loop index tracking the active column from 1 to i |
| sum | int | Stores the coordinate sum i + j to evaluate even or odd parity |
Build Confidence in ICSE Logic & Dry Runs
Master modulus parity logic, operator precedence, and ICSE dry-run grids with 4Achievers Junior (Grades 3-12) in 1-on-1 classes or 1:4 Micro-Pods.
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Checking j % 2 == 0 instead of (i + j) % 2 == 0, which prints 0 1 / 0 1 0 so every row starts with the exact same digit instead of alternating vertically.
- Avoid Mistake: Using i + j % 2 == 0 without parentheses around i + j, causing Java precedence to evaluate j % 2 before adding i.
- Avoid Mistake: Testing sum / 2 == 0 (division quotient) instead of sum % 2 == 0 (modulus remainder).
- Avoid Mistake: Forgetting to reset a toggle flag if attempting the non-modulus approach, breaking row-start alternation on even rows.
- Examiner / Mentor Tip: Remind examiners in your dry-run table that 2 % 2 == 0 evaluates to true while 3 % 2 == 0 evaluates to false.
- Examiner / Mentor Tip: Show why int sum = i + j prevents operator-precedence mistakes with % vs + if asked during BlueJ viva.
- Examiner / Mentor Tip: Check carefully in ICSE papers whether row 2 starts with 0 (checkerboard: 1 / 0 1 / 1 0 1) or with 1 (column-based: 1 / 1 0 / 1 0 1).
7. Interactive Concept Check Quiz
Question: What does row 4 (i = 4) of BinaryTriangle print when sum % 2 == 0 outputs 1?
8. Frequently Asked Questions
Why is storing int sum = i + j safer than writing i + j % 2 == 0?
How do I make the triangle start with 0 (0 / 1 0 / 0 1 0) instead of 1?
What pattern does j % 2 != 0 produce instead?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Palindromic Number Pyramid (1 / 121 / 12321) Java
- Butterfly Star Pattern in Java Using Nested Loops
- Fibonacci Series Program in Java (Iterative Terms & Sum)
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
