CJ_Slip_9A

Slip No.9

A) Write a java Program to display following pattern:
1
0 1
0 1 0
1 0 1 0

1. Open Notepad.

2. Type the following code:

    public class PatternDisplay {
    public static void main(String[] args) {
    int rows = 4; // Number of rows to display

    for (int i = 1; i <= rows; i++) {
    // Determine the starting number for the current row-
    int startNumber = (i % 2 == 0) ? 0 : 1;

    for (int j = 1; j <= i; j++) {
    System.out.print(startNumber + " ");
    // Alternate the number between 0 and 1
    startNumber = 1 - startNumber;
    }

    // Move to the next line
    System.out.println();
    }
    }
    }

3. Open the Command Prompt.

4. Compile the Java program by typing:
javac PatternDisplay.java

5. Run the compiled Java program by typing:
java PatternDisplay

No comments:

Post a Comment