5 different Java program to print a hollow square star pattern with diagonal

Hollow square star pattern with diagonal in Java:

In this post, we will learn how to write a Java program to print a hollow square with diagonals star pattern. I will show you how to write the algorithm for the pattern and how to write it by using a for loop, while loop, do-while loop and by using a separate function.

Algorithm to print a hollow square star pattern with diagonal:

A hollow square star pattern with diagonals of size 7 will look as like below:

* * * * * * * 
* *       * * 
*   *   *   * 
*     *     * 
*   *   *   * 
* *       * * 
* * * * * * * 

This pattern is printed with * and blank spaces. If I replace all blank spaces with #, it will be:

* * * * * * * 
* * # # # * * 
* # * # * # * 
* # # * # # * 
* # * # * # * 
* * # # # * * 
* * * * * * * 

Let’s say, we are using two loops to print this pattern. One will be the outer loop and another will be the inner loop.

  • Both of these loops will run for size number of times, where size is the size of the square. Suppose, the outer loop runs from i = 0 to i = size - 1 and the inner loop runs from j = 0 to j = size - 1.
  • The inner loop will print a * or blank space on each iteration.
  • It will print stars:
    • For the first row and last row, i.e. if i = 0 or i = size - 1.
    • For the first column and last column, i.e. if j = 0 or j = size - 1.
    • To print the left diagonal, we have to print stars if i == j.
    • To print the right diagonal, we have to print stars if j = size - i - 1

Let’s write down the program.

Method 1: Java program to print a hollow square star pattern with diagonal using for loops:

Let’s write down the Java program:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int size;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the square: ");
        size = scanner.nextInt();

        System.out.println("Hollow square pattern with diagonals of size " + size + ": \n");

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

In this program,

  • scanner is a Scanner object to read the user input values.
  • It asks the user to enter the size of the square, reads it and assign it to the size variable.
  • The outer for loop runs from i = 0 to i = size - 1. The inner for loop runs from j = 0 to j = size - 1.
  • The if block checks if the current values of i and j meet any of the conditions we discussed above. If it returns true, it prints a star. Else, if prints a blank space.
  • At the end of each iteration of the outer loop, one new line is printed to move to the next line.

If you run this program, it will give output as below:

Enter the size of the square: 7
Hollow square pattern with diagonals of size 7: 

* * * * * * * 
* *       * * 
*   *   *   * 
*     *     * 
*   *   *   * 
* *       * * 
* * * * * * * 

Enter the size of the square: 6
Hollow square pattern with diagonals of size 6: 

* * * * * * 
* *     * * 
*   * *   * 
*   * *   * 
* *     * * 
* * * * * * 

Method 2: Java program to print a hollow square star pattern with diagonal using while loops:

Similar to the above example, we can also use two while loops to write the same program in Java. Let’s write this program with two while loops:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int size, i = 0, j;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the square: ");
        size = scanner.nextInt();

        System.out.println("Hollow square pattern with diagonals of size " + size + ": \n");

        while (i < size) {
            j = 0;
            while (j < size) {
                if (i == 0 || i == size - 1 || j == 0 || j == size - 1 || i == j || j == size - i - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
                j++;
            }
            System.out.println();
            i++;
        }
    }
}

This is similar to the above program. The only difference is that we are using two while loops instead of for loops.

  • The variables, i and j are initialized before the loops started.
  • At the end of each iteration of the outer loop, the value of i is incremented by 1. Similarly, at the end of each iteration of the inner loop, the value of j is incremented by 1.
  • The value of j is reset to 0 before the inner loop starts.

If you run the program, it will give similar output.

### Method 3: Java program to print a hollow square star pattern with diagonal using do-while loops:

The do-while loops work similarly to the while loops. We can also use do-while loops to write the same program.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int size, i = 0, j;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the square: ");
        size = scanner.nextInt();

        System.out.println("Hollow square pattern with diagonals of size " + size + ": \n");

        do {
            j = 0;
            do {
                if (i == 0 || i == size - 1 || j == 0 || j == size - 1 || i == j || j == size - i - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
                j++;
            } while (j < size);
            System.out.println();
            i++;
        } while (i < size);
    }
}

Here,

  • The do block will run first and then it will check the while condition.

It works similar to the previous example and if you run, it will print similar output.

Enter the size of the square: 6
Hollow square pattern with diagonals of size 6: 

* * * * * * 
* *     * * 
*   * *   * 
*   * *   * 
* *     * * 
* * * * * * 

Enter the size of the square: 7
Hollow square pattern with diagonals of size 7: 

* * * * * * * 
* *       * * 
*   *   *   * 
*     *     * 
*   *   *   * 
* *       * * 
* * * * * * * 

### Method 4: Java program to print a hollow square star pattern with diagonal using a separate method:

We can write a separate method to print the pattern. Using a separate method is always useful as we can call this method from different classes. This method will take the size of the pattern as its parameter and it will print the pattern.

Below is the complete program:

import java.util.Scanner;

public class Main {
    private static void printSquarePattern(int size) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (i == 0 || i == size - 1 || j == 0 || j == size - 1 || i == j || j == size - i - 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int size;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the square: ");
        size = scanner.nextInt();

        System.out.println("Hollow square pattern with diagonals of size " + size + ": \n");

        printSquarePattern(size);
    }
}
  • printSquarePattern method is used to print the pattern. This method takes the size of the square as its parameter and it prints the hollow square pattern with diagonals.
  • It uses two for loops similar to the first example.

If you run this program, it will print similar output.

Method 5: Java program to print a hollow square star pattern with diagonal using user input character:

Let’s take the character to print the pattern as input from the user. The program will take the size of the square and the character as inputs to print the hollow square:

import java.util.Scanner;

public class Main {
    private static void printSquarePattern(int size, char c) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (i == 0 || i == size - 1 || j == 0 || j == size - 1 || i == j || j == size - i - 1) {
                    System.out.print(c + " ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int size;
        char c;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the size of the square: ");
        size = scanner.nextInt();

        System.out.print("Enter the character to print the pattern: ");
        c = scanner.next().charAt(0);

        System.out.println("Hollow square pattern with diagonals of size " + size + ": \n");

        printSquarePattern(size, c);
    }
}

Here, c is the variable to hold the user input character. The printSquarePattern method is changed to take both size and c as the parameters. It uses the character, c, to print the pattern.

Enter the size of the square: 7
Enter the character to print the pattern: $
Hollow square pattern with diagonals of size 7: 

$ $ $ $ $ $ $ 
$ $       $ $ 
$   $   $   $ 
$     $     $ 
$   $   $   $ 
$ $       $ $ 
$ $ $ $ $ $ $ 

Enter the size of the square: 6
Enter the character to print the pattern: ^
Hollow square pattern with diagonals of size 6: 

^ ^ ^ ^ ^ ^ 
^ ^     ^ ^ 
^   ^ ^   ^ 
^   ^ ^   ^ 
^ ^     ^ ^ 
^ ^ ^ ^ ^ ^ 

You might also like: