Alternating 1 and 0 Pattern with Decreasing Width in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
11111
0000
111
00
1
1

Complete 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.

Java
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

1

Set the size

rows = 5 sets the starting width and the number of lines.

Setup
2

Outer loop controls the row parity

Row i decides whether to print 1 or 0 using i % 2.

Row control
3

Inner loop shrinks the width

for (j = i; j <= rows; j++) runs rows - i + 1 times, so each next row is shorter.

Width
4

Print 1s and 0s based on even/odd

If i is even, print 0; otherwise print 1. That produces alternating lines.

Logic
=

Alternating decreasing rows

Total printed characters are \(rows + (rows-1) + \dots + 1\), so runtime grows like O(n²).

2

Variation — User Input Version

Let the user choose the number of rows using Scanner:

Java
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 with StringBuilder in Java
  • Make the width constant by looping j from 1..rows
  • Turn it into a 0/1 staircase by printing j from 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

1

Odd rows print 1; even rows print 0 using i % 2.

2

The width decreases because the inner loop prints rows-i+1 characters.

3

Total printed characters are n(n+1)/2, so runtime grows like O(n²).

4

This is a common pattern template: combine row parity + decreasing width.

❓ Frequently Asked Questions

On row 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.
Swap the outputs in the if/else (print 0 on odd rows and 1 on even rows).
Yes. Print "1 " and "0 " instead of "1" and "0". Note that the output formatting will change.
O(n²) because total prints are n+(n-1)+...+1.

Explore More Java Number Patterns!

Alternating patterns are a great way to practice parity checks and nested loop bounds.

All Number Patterns →
Did you know?

Many “alternating” patterns can be built with a single parity check such as i % 2 (row-based) or (i + j) % 2 (checkerboard-style).

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