Java Binary Rows Pattern (Alternating 1 and 0)

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

What Is This Pattern?

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.

How to Solve It

Walk i from 1 to rows; pick 1 or 0 from i % 2, then repeat it rows − i + 1 times.

MethodIdeaBest for
Parity + nested loopsi % 2 picks the digit; inner loop sets widthLearning, interviews, exams
Spaced digitsSame loops; print each digit with a trailing spaceReadable 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

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Pick digiti % 2 == 0 ? "0" : "1"
Shrinking widthfor (int j = i; j <= rows; j++)
Print digitSystem.out.print(digit);
End the rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach repeated digit
System.out.printlnEnds the current lineAfter the inner loop finishes

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

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 result rows = 5 · 15 digits
11111
0000
111
00
1

Worked Walkthrough — rows = 5

Trace the digit from i % 2 and the width rows − i + 1 for each row.

iParityDigitWidthPrinted row
1odd1511111
2even040000
3odd13111
4even0200
5odd111

Total digits = 5 + 4 + 3 + 2 + 1 = 15 — the triangular number n(n+1)/2.

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();
        }
    }
}

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Alternating 1/0 (Examples 1–3)O(n²)O(1)

Total digits = n(n+1)/2 — still quadratic. Only loop counters are stored.

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.

Next: Square Number Pyramid

Move on to the square number pyramid in the Java number-pattern series.

Program 41 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