Java Pattern Programs

Write a program in Java to display the following pattern:

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

Java

Java Nested for Loops

6 Likes

Answer

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

Output

Answered By

2 Likes