Java Binary Number Triangle Pattern (Alternating)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An alternating binary number triangle grows one digit per row, printing 0 or 1 from j % 2 as the inner index counts down.

Remember
Rule: outer i = 1..rows; inner j = i..1; print j % 2

1
01
101
0101
10101     ← rows = 5

Unlike Program 16 (ascending inner, rows start with 1) or decimal patterns that print j itself, this pattern turns every column into a binary bit with modulo.

How to Solve It

Outer loop from 1 to rows. Inner loop from current i down to 1, printing j % 2. End the line after the inner loop.

MethodIdeaBest for
Descending + j % 2Inner i..1, print parityLearning modulo patterns
FlipPrint 1 - (j % 2)Starting each row with 0

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i down to 1:
        print j % 2
    print newline

Cheat sheet

GoalPattern
Outer (grow length)for (int i = 1; i <= rows; i++)
Inner (count down)for (int j = i; j >= 1; j--) System.out.print(j % 2);
End the rowSystem.out.println();
Flip every bitSystem.out.print(1 - (j % 2));
Ascending insteadfor (j = 1; j <= i; j++) print(j % 2) → Program 16 shape
Digits on row ii

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach bit j % 2
System.out.printlnEnds the current lineAfter the inner loop

Print bits without a newline, then end the row once.

Live Preview

Change the row count and the binary triangle updates instantly.

Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 digits
1
01
101
0101
10101

Worked Walkthrough

Trace three outer values when rows = 5 — watch j % 2 flip as j counts down.

Outer iInner jj % 2Prints
1111
33, 2, 11, 0, 1101
55..11, 0, 1, 0, 110101

Odd j prints 1; even j prints 0. The descending order is what puts a leading 0 on even-length rows.

Java Programs

Three complete programs: fixed rows = 5, a flipped-bit variant, and Scanner input. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer grows, inner counts down, print j % 2.

Java
public class AlternatingBinaryTriangle {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j >= 1; j--) {
                System.out.print(j % 2);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer grows the length. i runs from 1 to rows — that is how many bits each row gets.

2. Inner prints parity. j counts down from i to 1; j % 2 is the bit.

3. End the row. Call System.out.println() only after the inner loop finishes.

Example 2 — Flip with 1 - (j % 2)

Invert every bit so the first row starts with 0 instead of 1.

Java
public class AlternatingBinaryFlipped {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j >= 1; j--) {
                System.out.print(1 - (j % 2));
            }
            System.out.println();
        }
    }
}

How It Works

1. Same loop bounds. Outer and inner ranges match Example 1.

2. Flip the bit. 1 - (j % 2) turns 1 into 0 and 0 into 1.

3. First row becomes 0. Useful when a lab asks for the complementary binary triangle.

Example 3 — User Input Rows

Read the row count at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

public class AlternatingBinaryInput {
    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 >= 1; j--) {
                System.out.print(j % 2);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Same loop core. Only the source of rows changes from a literal to Scanner.

2. Entering 4 stops early. You get four rows ending at 0101.

3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive height (tip below).

Safer input tip
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Edge Cases & Pitfalls

Check these before calling the solution done.

Print value

Print j % 2, not j

Printing j reprints a reverse decimal triangle, not binary bits.

Inner direction

Count down with j--

Counting up reprints Program 16 (1, 10, 101, 1010), not this shape.

println inside

Do not put println inside the inner loop

That prints one bit per line and destroys the triangle.

Bad input

Validate with hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Binary triangle (Examples 1–3)O(n²)O(1)

Bits printed are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Extra memory is only the loop variables.

Key Takeaways

  • Rule: outer i = 1..rows, inner j = i..1, print j % 2.
  • Modulo: odd j → 1, even j → 0.
  • Break the row: call println only after the inner loop finishes.
  • Complexity: O(n²) time, O(1) extra space.

One line: grow the row, count j down, and print each bit with j % 2.

Frequently Asked Questions

An alternating binary triangle: for rows=5 you get 1, 01, 101, 0101, 10101.
Modulo 2 returns the remainder after dividing by 2. Any integer is either even (remainder 0) or odd (remainder 1).
Row 3 prints j = 3, 2, 1 and j % 2 becomes 1, 0, 1 — concatenated as 101.
System.out.print(j % 2) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Program 15 counts the inner loop down (1, 01, 101). Program 16 counts up and starts each row with 1 (1, 10, 101, 1010).
Yes. Print 1 - (j % 2) instead of j % 2 to flip every digit — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.

Did you know?

Each row prints alternating 0 and 1 using j % 2. The inner loop counts down from i to 1, so row length grows each line — still O(n²) total prints.

Next: Binary Triangle Starting with 1

Continue with the next pattern in the Java number-pattern series.

Program 16 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful