Print 0-1 Triangle in Java
Problem
A 0-1 triangle alternates its value based on both the row and column together, so consecutive positions — across a row and down a column — never share the same value.
Given a number of rows, print a triangle where the entry at row i, column j is (i + j) modulo 2.
Java Program
public class ZeroOneTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
StringBuilder line = new StringBuilder();
for (int j = 1; j <= i; j++) {
if (j > 1) line.append(" ");
line.append((i + j) % 2); // flips whenever either i or j changes
}
System.out.println(line);
}
}
}Output
Core Logic
Adding the row and column positions together before taking the result modulo 2 flips the printed value every time either one changes, producing a genuine checkerboard instead of a row-by-row alternation.
- 1The outer loop runs
ifrom 1 ton, and the inner loop runsjfrom 1 toi, visiting every position. - 2
(i + j) % 2is computed fresh at each position — since eitheriorjchanging by one flips whether the sum is even or odd, the printed value flips too. - 3Moving one step to the right within a row flips the value, and moving one row down at the same column also flips it — that's what makes this a checkerboard rather than the row-only alternation of a binary triangle.
- 4No stored state is needed between positions — each cell's value is computed independently from just its own
iandj.
n = 5, row 3 (i = 3) prints 0 1 0 — position (3,1) sums to 4 (even, 0), (3,2) sums to 5 (odd, 1), (3,3) sums to 6 (even, 0).Key Point: This is the direct counterpart to the binary triangle — that one alternates only between rows, this one alternates between both rows and columns, at every single position.
Why: Every position in the triangular grid is visited once to compute its own value directly, a total proportional to n², with no stored state carried between cells.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ZeroOneTriangleStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.mapToObj(i -> IntStream.rangeClosed(1, i)
.mapToObj(j -> String.valueOf((i + j) % 2))
.collect(Collectors.joining(" ")))
.forEach(System.out::println);
}
}
Output
Core Logic
Since each position's value depends only on its own row and column, mapping every column index directly to (i + j) % 2 reproduces the checkerboard without any state carried between positions.
- 1
IntStream.rangeClosed(1, n)generates one stream element per rowi. - 2For each row, an inner
IntStream.rangeClosed(1, i)generates that row's column positionsj. - 3
.mapToObj(j -> String.valueOf((i + j) % 2))computes each position's value directly from its owniandj, the same formula the loop version uses. - 4
Collectors.joining(" ")joins that row's values beforeforEachprints it.
j = 1, 2, 3 to (3+1)%2, (3+2)%2, (3+3)%2, giving "0 1 0".Key Point: Because (i + j) % 2 reads only its own two parameters, this maps cleanly onto a stream even though the value depends on both dimensions at once — no shared counter or lookback into a previous cell is needed.
Why: The nested streams still compute one value per grid position across all rows, proportional to n², without collecting the full triangle.