Java Program to Print Hollow Square with Diagonals

Write a Java Program to print hollow square with diagonals star pattern using for loop. This Java hollow square example uses nested for loop to iterate the square sides. The If else condition check whether it is first row or column, last row or column, or diagonal. If it is true, it prints a star at that position; otherwise, print empty space.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquareDiagonal1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Hollow Square with Diagonals Side = ");
		int side = sc.nextInt();	
		
		System.out.println("Hollow Square Star with Diagonals Pattern");	
		
		for (int i = 1; i <= side; i++ ) 
		{
			for (int j = 1 ; j <= side; j++ ) 
			{
				if (i == 1 || i == side || i == j || 
						j == 1 || j == side || j == side - i + 1) {
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Hollow Square with Diagonals

In this Java program to display the hollow square with the diagonals pattern of stars, we replaced the for loop with nested while loops.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquareDiagonal2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square with Diagonals Side = ");
		int side = sc.nextInt();
		
		System.out.println("Hollow Square Star with Diagonals Pattern");
		int i = 1, j; 
		while (i <= side) 
		{
			j = 1 ;
			while( j <= side) 
			{
				if (i == 1 || i == side || i == j || 
						j == 1 || j == side || j == side - i + 1) {
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
				j++ ;
			}
			System.out.println();
			i++ ;
		}
	}
}
Enter Hollow Square with Diagonals Side = 10
Hollow Square Star with Diagonals Pattern
* * * * * * * * * * 
* *             * * 
*   *         *   * 
*     *     *     * 
*       * *       * 
*       * *       * 
*     *     *     * 
*   *         *   * 
* *             * * 
* * * * * * * * * * 

Java Program to Print Hollow Square with Diagonals Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class HollowSquareDiagonal3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square with Diagonals Side = ");
		int side = sc.nextInt();
		
		System.out.println("Hollow Square Star with Diagonals Pattern");
		int i = 1, j; 
		do 
		{
			j = 1 ;
			do
			{
				if (i == 1 || i == side || i == j || 
						j == 1 || j == side || j == side - i + 1) {
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
			} while( ++j <= side) ;
			System.out.println();
		} while (++i <= side);
	}
}
Enter Hollow Square with Diagonals Side = 14
Hollow Square Star with Diagonals Pattern
* * * * * * * * * * * * * * 
* *                     * * 
*   *                 *   * 
*     *             *     * 
*       *         *       * 
*         *     *         * 
*           * *           * 
*           * *           * 
*         *     *         * 
*       *         *       * 
*     *             *     * 
*   *                 *   * 
* *                     * * 
* * * * * * * * * * * * * * 

This Java code prints a hollow square with the diagonals pattern of the user-given symbol. We created a HollowSquareDiagonalPattern function that prints the given symbol in a hollow square with a diagonals pattern.

package ShapePrograms;

import java.util.Scanner;

public class HollowSquareDiagonal4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Square with Diagonals Side = ");
		int side = sc.nextInt();
		
		System.out.print("Character for Hollow Square Diagonal Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Hollow Square Star with Diagonals Pattern");
		HollowSquareDiagonalPattern(side, ch);
	}
	
	public static void HollowSquareDiagonalPattern(int side, char ch) {
		for (int i = 1; i <= side; i++ ) 
		{
			for (int j = 1 ; j <= side; j++ ) 
			{
				if (i == 1 || i == side || i == j || 
						j == 1 || j == side || j == side - i + 1) {
					System.out.print(ch + " ");
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}
Enter Hollow Square with Diagonals Side = 20
Character for Hollow Square Diagonal Pattern = $
Hollow Square Star with Diagonals Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$ $                                 $ $ 
$   $                             $   $ 
$     $                         $     $ 
$       $                     $       $ 
$         $                 $         $ 
$           $             $           $ 
$             $         $             $ 
$               $     $               $ 
$                 $ $                 $ 
$                 $ $                 $ 
$               $     $               $ 
$             $         $             $ 
$           $             $           $ 
$         $                 $         $ 
$       $                     $       $ 
$     $                         $     $ 
$   $                             $   $ 
$ $                                 $ $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $