OneCompiler

Kshapepattern

143

public class Main {

// Main Method
public static void main(String[] args) {

    // Initializing required number of lines/rows
    int n = 5;

    // Upper half
    // Outer loop for the line/row change
    for (int i = 1; i <= n; i++) {

        // Inner loop for the star printing
        for (int j = 1; j <= n; j++) {

            if (j == 1 || i + j == n + 1) {
                // Printing the star without changing the line
                System.out.print(j+" ");
            } else {
                // Printing the space without changing the line
                System.out.print(" ");
            }
        }

        // Line/Row change
        System.out.println();
    }

    // Lower half
    // Outer loop for the line/row change
    for (int i = 2; i <= n; i++) {

        // Inner loop for the star printing
        for (int j = 1; j <= n; j++) {

            if (j == 1 || i - j == 0) {
                // Printing the star without changing the line
                System.out.print(j + " ");
            } else {
                // Printing the space without changing the line
                System.out.print(" ");
            }
        }

        // Line/Row change
        System.out.println();
    }
}

}