KnowledgeBoat Logo

Java Pattern Programs

Write a program in Java to display the following pattern:

0 1 0 1 
1 0 1 0 
0 1 0 1 
1 0 1 0

Java

Java Nested for Loops

8 Likes

Answer

public class KboatPattern
{
    public static void main(String args[]) {
        for (int i = 1; i <= 4; i++) {
            int t;
            if (i % 2 == 0)
                t = 1;
            else
                t = 0;
                
            for (int j = 1; j <= 4; j++) {
                System.out.print(t + " ");
                if (t == 1)
                    t = 0;
                else
                    t = 1;
            }
            System.out.println();
        }
    }
}

Output

BlueJ output of Write a program in Java to display the following pattern: 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0

Answered By

5 Likes