Alternating 1 and 0 Pattern with Decreasing Width in Java

What You’ll Learn
How to print an alternating 1/0 pattern in Java where the width decreases each row:
11111, then 0000, then 111, then 00, then 1.
You’ll use nested loops and an even/odd row check (i % 2) to choose whether to print 1 or 0.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
11111
0000
111
00
1Complete Java Program
If the row number i is even, print 0; otherwise print 1. The inner loop shrinks because it runs from j=i to rows.
public class Main {
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
Set the size
rows = 5 sets the starting width and the number of lines.
Outer loop controls the row parity
Row i decides whether to print 1 or 0 using i % 2.
Inner loop shrinks the width
for (j = i; j <= rows; j++) runs rows - i + 1 times, so each next row is shorter.
Print 1s and 0s based on even/odd
If i is even, print 0; otherwise print 1. That produces alternating lines.
Alternating decreasing rows
Total printed characters are \(rows + (rows-1) + \dots + 1\), so runtime grows like O(n²).
Variation — User Input Version
Let the user choose the number of rows using Scanner:
import java.util.Scanner;
public class Main {
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 <= rows; j++) {
System.out.print(i % 2 == 0 ? "0" : "1");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Flip the pattern by swapping the digits (print 0 on odd rows)
- Print spaces between digits for a more “grid-like” view
- Use
String.repeat()(in other languages) or build a row withStringBuilderin Java - Make the width constant by looping
jfrom 1..rows - Turn it into a 0/1 staircase by printing
jfrom 1..i
Avoid
- Hard-coding 5 everywhere instead of using
rows - Using complex conditions when a simple parity check works
- Forgetting the newline after each row
- Printing extra whitespace that changes the expected output
Key Takeaways
Odd rows print 1; even rows print 0 using i % 2.
The width decreases because the inner loop prints rows-i+1 characters.
Total printed characters are n(n+1)/2, so runtime grows like O(n²).
This is a common pattern template: combine row parity + decreasing width.
❓ Frequently Asked Questions
i=2, the inner loop runs from j=2 to 5, which is 4 iterations, and since i is even it prints 0 each time."1 " and "0 " instead of "1" and "0". Note that the output formatting will change.Explore More Java Number Patterns!
Alternating patterns are a great way to practice parity checks and nested loop bounds.
Many “alternating” patterns can be built with a single parity check such as i % 2 (row-based) or (i + j) % 2 (checkerboard-style).
12 people found this page helpful
