Wednesday, 25 October 2023

Write a java Program to display following Pattern: 1 0 1 0 1 0 1 0 1 0-Core Java Slip 9

 

Write a java Program to display following Pattern:

1

0 1

0 1 0

1 0 1 0

public class PatternDisplay {

    public static void main(String[] args) {

        int n = 4; // Number of rows in the pattern

         for (int i = 0; i < n; i++) {

            int numToPrint = (i % 2 == 0) ? 1 : 0;

            for (int j = 0; j <= i; j++) {

                System.out.print(numToPrint + " ");

                numToPrint = 1 - numToPrint;

            }

            System.out.println();

        }

    }

}

 Output:-

C:\Program Files\Java\jdk-11.0.17\bin>javac PatternDisplay.java

C:\Program Files\Java\jdk-11.0.17\bin>java PatternDisplay

1

0 1

1 0 1

0 1 0 1

 

C:\Program Files\Java\jdk-11.0.17\bin>