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

Java Program to Print the Mirrored Half Diamond Star Pattern

In this tutorial, we will see how to print the mirrored half 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 mirrored half 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: 6

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 Mirrored Half Diamond Star Pattern

In this program, we will see how to print the mirrored half diamond star pattern in java using for 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 for loops to print the pattern.
  6. The first outer for loop iterates until the condition i<=n is false, the first inner loop displays space until the condition j<=n-i is false.
  7. The second inner loop will display the character until the condition j<i is false. The 1st outer loop will display this pattern after all iterations.
  8. The second outer loop iterates until the condition i>0 is false, the first inner loop will display space until j<=n-i is false. The 2nd inner loop will display character until the condition j<=i is false.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

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


Enter the number of rows: 6
*
**
***
****
*****
******
*****
****
***
**
*

Program 2: Print the Mirrored Half Diamond Star Pattern

In this program, we will see how to print the mirrored half diamond star 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 for loops to print the pattern.
  6. The first outer while loop will execute the code until the condition i<=n is false. The first inner loop will display the space until the condition j++<=n-i is false, the second inner loop will display the character until the condition j++<=i is false. The first outer loop will display the first half of the pattern of mirrored half diamond star pattern.
  7. After the first outer loop execution, the cursor comes to the next line and executes the second outer while loop.
  8. The second outer loop will execute the statements until the condition i>0 is false, the first inner loop displays the space until the condition j++<=n-i is false, the second inner loop will display character until the condition j++<=i is false.
  9. The second outer loop will display the remaining half of the pattern of a mirrored half diamond.
  10. Stop

The below example illustrates the implementation of the above algorithm.

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


Enter the number of rows: 6
*
**
***
****
*****
******
*****
****
***
**
*

Program 3: Print the Mirrored Half Diamond Star Pattern

In this program, we will see how to print the mirrored half diamond star pattern in java using a do-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 do-while loops to print the pattern.
  6. The do-while loop executes the code one time then checks the condition at the while.
  7. The first outer do-while loop executes the code until the condition is false ++i<=n is false, first inner loop will display the space until the condition ++j<=n-i+1 is false, second inner loop will display character until the condition ++j<=i is false. This outer loop displays half of the pattern of a mirrored half diamond pattern.
  8. The second outer do-while loop executes the code until the condition–i>0 is false, first inner loop will display space until the condition ++j<=n-i+1, second inner loop will display character until the condition ++j<=i is false. This outer loop displays the remaining half of the pattern of a mirrored half diamond pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

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


Enter the number of rows: 6
*
**
***
****
*****
******
*****
****
***
**
*