Binary 1-0-1 Triangle Pattern Program in Java

⚡ Direct Answer & Key Takeaway

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

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 NameData TypePurpose in Program
scScannerReads the number of rows entered by the user from standard input
rowsintStores the height of the binary triangle pattern
iintOuter loop index tracking the active row from 1 to rows
jintInner loop index tracking the active column from 1 to i
sumintStores the coordinate sum i + j to evaluate even or odd parity
🎯 1-on-1 Live Mentorship & Free Study Kit

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.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

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?
Parentheses are mandatory if you write (i + j) % 2 == 0 inline, because % has higher precedence than +. Storing int sum = i + j beforehand avoids operator precedence bugs entirely.
How do I make the triangle start with 0 (0 / 1 0 / 0 1 0) instead of 1?
Invert the if condition to check sum % 2 != 0 (or swap "0 " and "1 " inside the if-else branches) so row 1 (1+1=2, even) prints 0 first.
What pattern does j % 2 != 0 produce instead?
Checking j % 2 != 0 produces 1 / 1 0 / 1 0 1 / 1 0 1 0 where every row begins with 1, rather than the alternating first column of a checkerboard binary triangle.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class