Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Print Hollow Diamond Star Pattern

In this tutorial, we will see how to print the hollow diamond star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the hollow diamond star pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 4

Output: The pattern is:

********

*** ***

** **

* *

* *

** **

*** ***

********

This can be done by using the following methods:

Approach 1: Using a For Loop

Approach 2: Using a While loop

Approach 3: Using a do-while loop

Let us look at each of these approaches for a better understanding

Program 1: Print the Hollow Diamond Star Pattern

In this program, we will see how to print the hollow diamond star pattern in java using a for loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a for loop to print the pattern.

  6. The first outer for loop will display the half of the hollow diamond pattern, the second outer for loop will display the remaining hollow diamond pattern.

  7. The first outer for loop executes the set of statements until the condition i<n is false, if the condition is true the first inner loop will be executed until the condition is true, then it checks the if condition true it will display character “*”, otherwise else part will display space.

  8. Next second inner loop will be executed until the condition is false. For i=0 the first row of the hollow pattern completed. The first for loop will display this pattern.

  9. The second outer for loop executes the statements until the condition is false. The first inner loop executes the statements until the condition is false. In this loop, if the “if” condition is true then it displays the character “*” otherwise it displays space.

  10. The second inner loop executes the set of statements until the condition is false, in the second inner loop if the “if” condition true it displays space else it displays the character “*”. The second outer loop will display this pattern.

  11. Display the result

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j<n-i)
                System.out.print("*");
                else
                System.out.print(" ");
            }
            for(int j=0;j<n;j++)
            {
                if(j<i)
                System.out.print(" ");
                else
                System.out.print("*");
            }
            System.out.println();
        } 
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j<i)
                System.out.print("*");
                else
                System.out.print(" ");
            }
            for(int j=0;j<n;j++)
            {
                if(j<n-i)
                System.out.print(" ");
                else
                System.out.print("*");
            }
            System.out.println();
        } 
     }
}


Enter the number of rows: 7
**************
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
**************

Program 2: Print the Hollow Diamond Star Pattern

In this program, we will see how to print the hollow diamond star pattern in java using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a while loop to print the pattern.

  6. The first outer while loop will execute the code until the condition is true. The 1st inner while loop executes the statements until the condition j<n.

  7. In the first inner while loop if j<n-i is true it will display character, else it displays space.

  8. After the first inner loop execution, the second inner loop will execute the statements until the condition j<n is false. In the second inner loop, if the condition is j<i is true, it will display space else it will display character”*”.

  9. The second outer while loop will execute the statements until the condition i<=n is false. The first inner loop executes the statements until the condition j<n is false, in this loop if the “if” condition is true then displays character “*” otherwise displays space.

  10. After the first inner loop execution, the second inner loop will be executed until the condition j<n is false. In this second inner loop if the “if” condition is true j<n-i it will display space otherwise it will display character.

  11. Display the result

  12. Stop

    The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=0;
        int j;
        while(i<n)
        {
            j=0;
            while(j<n)
            {
                if(j<n-i)
                System.out.print("*");
                else
                System.out.print(" ");
                j++;
            }
            j=0;
            while(j<n)
            {
                if(j<i)
                System.out.print(" ");
                else
                System.out.print("*");
                j++;
            }
            System.out.println();
            i++;
        } 
        i=1;
        while(i<=n)
        {
            j=0;
            while(j<n)
            {
                if(j<i)
                System.out.print("*");
                else
                System.out.print(" ");
                j++;
            }
            j=0;
            while(j<n)
            {
                if(j<n-i)
                System.out.print(" ");
                else
                System.out.print("*");
                j++;
            }
            System.out.println();
            i++;
        }
     }
}


Enter the number of rows: 7
**************
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
**************

Program 3: Java Program to Print the Hollow Diamond Star Pattern

In this program, we will see how to print the hollow diamond star pattern in java using a do-while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a do-while loop to print the pattern.

  6. The first outer do-while loop will execute the code until the condition i<n is false. The first inner loop executes until j<n is false. In the first inner loop if j<n-i is true it will display character “*” otherwise it will display space.

  7. After the first inner loop execution, the second inner loop will be executed until the condition j<n is false, in this loop if the if condition j<i true it prints space otherwise prints character “*”.

  8. The second outer do-while loop will be executed until the condition i<=n. The first inner loop executes the statements until the condition is false. In this loop, if condition j<i true then prints character otherwise prints space.

  9. After completion of the first inner loop, the second inner loop will be executed until the condition j<i is false. The if condition j<n-i true then prints space otherwise prints the character.

  10. Display the result

  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Diamond Star Pattern
import java.util.*;
public class Main
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int n=sc.nextInt(); 
        int i=0;
        int j;
        do
        {
           j=0;
           do
           {
                if(j<n-i)
                System.out.print("*");
                else
                System.out.print(" ");
                j++;
            }while(j<n);
            j=0;
            do
            {
                if(j<i)
                System.out.print(" ");
                else
                System.out.print("*");
                j++;
            }while(j<n);
            System.out.println();
            i++;
        } while(i<n);
        i=1;
        do
        {
           j=0;
           do
           {
                if(j<i)
                System.out.print("*");
                else
                System.out.print(" ");
                j++;
           } while(j<n);
           j=0;
           do
           {
                if(j<n-i)
                System.out.print(" ");
                else
                System.out.print("*");
                j++;
            }while(j<n);
            System.out.println();
            i++;
        }while(i<=n);
     }
}


Enter the number of rows: 7
**************
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
**************