Java Program to Print Hollow Diamond Pattern inside a Square

Write a Java Program to print the hollow diamond pattern of stars inside a square pattern using a while loop. Here, we used multiple while loop and if-else statements to print the hollow diamond pattern printed inside a square pattern.

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Diamond inside a Square Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
		i = 0 ;
		while( i < rows) 
		{
			j = 0 ;
			while ( j < rows  ) 
			{
				if(j < rows - i) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
				j++;
			}
			k = 0 ;
			while ( k < rows) 
			{
				if (k < i ) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				k++ ;
			}
			System.out.println();
			i++;
		}
		
		i = 1 ;
		while ( i <= rows ) 
		{
			j = 0 ;
			while ( j < rows ) 
			{
				if(j < i) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
				j++;
			}
			k = 0 ;
			while ( k < rows) 
			{
				if (k < rows - i ) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				k++ ;
			}
			System.out.println();
			i++;
		}
	}
}
Enter Hollow Diamond inside a Square Rows = 8
Printing Hollow Diamond Star Pattern inside a Square
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************

In this Java hollow diamond star pattern inside a square program, we alter the above code to remove all the if-else statements.

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Diamond inside a Square Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = i ; j <= rows; j++ ) 
			{
				System.out.print("*");
			}
			for (j = 1 ; j <= 2 * i - 2; j++ ) 
			{
				System.out.print(" ");
			}
			for (k = i ; k <= rows; k++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 1 ; j <= i; j++ ) 
			{
				System.out.print("*");

			}
			for (k = 2 * i - 2 ; k < 2 * rows - 2; k++ ) 
			{
				System.out.print(" ");
			}
			for (k = 1 ; k <= i; k++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
Java Program to Print Hollow Diamond Pattern inside a Square 1

This Java hollow diamond Pattern inside a square program is the same as the first example, and we replaced the while loop with for loop.

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Diamond inside a Square Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
		
		for (i = 0 ; i < rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < rows - i) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
			}
			for (k = 0 ; k < rows; k++ ) 
			{
				if (k < i ) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
			}
			System.out.println();
		}
		
		for (i = 1 ; i <= rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < i) {
					System.out.print("*");
				}
				else {
					System.out.print(" ");
				}
			}
			for (k = 0 ; k < rows; k++ ) 
			{
				if (k < rows - i ) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
			}
			System.out.println();
		}
	}
}
Enter Hollow Diamond inside a Square Rows = 12
Printing Hollow Diamond Star Pattern inside a Square
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
*                      *
**                    **
***                  ***
****                ****
*****              *****
******            ******
*******          *******
********        ********
*********      *********
**********    **********
***********  ***********
************************