Header Ads Widget

Responsive Advertisement

Various Partterns in JAVA


 public class rectangle {

    public static void main(String args[]) {

        int n = 6;
        int m = 5;

        // 1. Question Solid Rectangle

        // *****
        // *****
        // *****
        // *****
        // *****
        // *****

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        System.out.println();

        // 2. Question Hollow Rectangle

        // *****
        // *   *
        // *   *
        // *   *
        // *   *
        // *****

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (i == 1 || j == 1 || i == n || j == m) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

        System.out.println();

        // 3. Question Half Pyramid

        // *
        // **
        // ***
        // ****
        // *****
        // ******

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        // System.out.println();

        System.out.println();

        // 4. Question inverted half pyramid

        // ******
        // *****
        // ****
        // ***
        // **
        // *

        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        System.out.println();

        // 5. Question 180 degree inverted pyramid

    //      *
    //     **
    //    ***
    //   ****
    //  *****
    // ******

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

            // This for loop for printing spaces
            for (int k = 1; k <= n - i; k++) {
                System.out.print(" ");
            }

            // This for loop for printing star
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        System.out.println();

        // 6. Question half Pyramid with Numbewrs

        // 1
        // 12
        // 123
        // 1234
        // 12345
        // 123456

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        System.out.println();

        // 7. Question Inverted half Pyramid with Numbewrs

        // 123456
        // 12345
        // 1234
        // 123
        // 12
        // 1

        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        System.out.println();

        // 8. Question Floyed's Triangle

        // 1
        // 2 3
        // 4 5 6
        // 7 8 9 10
        // 11 12 13 14 15
        // 16 17 18 19 20 21
       
        int number = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(number + " ");
                number++;
            }
            System.out.println();
        }

        System.out.println();

        // 9. Question 0 1 Triangle

        // 1
        // 0 1
        // 1 0 1
        // 0 1 0 1
        // 1 0 1 0 1
        // 0 1 0 1 0 1

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                int sum = i + j;
                if ((sum) % 2 == 0) {
                    System.out.print("1 ");
                } else {
                    System.out.print("0 ");
                }
            }
            System.out.println();
        }

    }
}

Post a Comment

0 Comments