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

Java Program to Print the Hollow Diamond Pattern

In this tutorial, we will see how to print the hollow diamond pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print two hollow equilateral triangles facing away from each other but with the same base. 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 For Loop

Approach 2: Using While loop

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

Program 1: Print the Hollow Diamond Pattern

In this program, we will see how to print the hollow diamond 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 two for loops to print the pattern.
  6. Inside each for loop use two more for loops and an if statement to print the spaces and pattern.
  7. Use the first for loop to print the upper hollow diamond pattern.
  8. Use the second for loop to print the lower hollow diamond pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
       int i,j;
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter the number of rows");
       int rows=scan.nextInt();//get input from user
       //print upper triangle
       for(i=1; i<=rows; i++)
       {
            for(j=rows; j>i; j--)
            {
                System.out.print(" ");//print space
            }
            System.out.print("*");  //print star
            for(j=1; j<(i-1)*2; j++)
            {
                System.out.print(" ");
            }
            if(i==1)
            {
                System.out.print("\n");//move to next line
            }
            else
            {
                System.out.print("*\n");
            }
        }
        //print lower triangle
        for(i=rows-1; i>=1; i--)
        {
            for(j=rows; j>i; j--)
            {
                System.out.print(" ");
            }
            System.out.print("*");
            for(j=1; j<(i-1)*2; j++)
            {
                System.out.print(" ");
            }
            if(i==1)
            {
                System.out.print("\n");
            }
            else
            {
                 System.out.print("*\n");
            }
        }
    }
}


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

Program 2: Print the Hollow Diamond Pattern

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

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use two while loops to print the pattern.
  6. Inside each while loop use two more while and an if statement to print the spaces and pattern.
  7. Use the first while loop to print the upper hollow diamond pattern.
  8. Use the second while loop to print the lower hollow diamond pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the hollow diamond pattern
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
       int i,j;
       Scanner scan=new Scanner(System.in);
       System.out.println("Enter the number of rows");
       int rows=scan.nextInt();   //get input from user
       i=1; 
       //print upper triangle
       while(i<=rows)
       {
           j=rows;
           while(j>i)
           {
              System.out.print(" ");
              j--;
           }
           System.out.print("*");
           j=1; 
           while(j<(i-1)*2)
           {
              System.out.print(" ");
              j++;
           }
           if(i==1)
           {
              System.out.print("\n");
           }
           else
           {
              System.out.print("*\n");
           }
           i++;
        }
        //print lower triangle
        i=rows-1; 
        while(i>=1)
        {
            j=rows; 
            while(j>i)
            {
                System.out.print(" ");   //print space
                j--;
            }
            System.out.print("*");     //print star
            j=1; 
            while(j<(i-1)*2)
            {
                System.out.print(" ");
                j++;
            }
            if(i==1)
            {
                System.out.print("\n");   //move to next line
            }
            else
            {
                System.out.print("*\n");
            }
            i--;
        }
    }
}


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