Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Print Hollow Mirrored Rhombus Star Pattern

In this tutorial, we will see how to print the hollow mirrored rhombus 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 mirrored rhombus star patterns. 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:

* * * *

* *

* *

* * * *

Program 1: Print the Hollow Mirrored Rhombus

In this program, we will see how to print the hollow mirrored rhombus star pattern using a 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 a for loop to print the pattern.
  6. Inside this for loop use two more for loop and another if-else statement to print the pattern.
  7. Use the first for loop to print the space and the other for loop to print the pattern.
  8. If if(i==1 || i==n) is true, then for loop with the structure for(int j=1;j<=n;j++) and prints characters from j=1 to n.
  9. And if it is false,the loop with the structure for(int j=1;j<=n;j++) and prints characters at j=1 or j=n,prints spaces at if j!=1 and j!=n .
  10. Display the output.
  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print Hollow Mirrored Rhombus
import java.util.Scanner;
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();	 
        System.out.println("Enter the Symbol : ");
        char ch = sc.next().charAt(0);
	    for(int i=1;i<=n;i++)
        {
            for(int j=i;j>0;j--)
            {
                System.out.print(" ");
            }
	        if(i==1 || i==n)
            for(int j=1;j<=n;j++)
            {
               System.out.print(ch);
            }
            else
	        {
	            for(int j=1;j<=n;j++)
                {  
            		if(j==1 || j==n)
            		System.out.print(ch);
                  	else
                    System.out.print(" ");
                }
            }
            System.out.println();
       }            
    }
}


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

Program 2: Print the Hollow Mirrored Rhombus

In this program, we will see how to print the hollow mirrored rhombus star pattern 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 a while loop to print the pattern.

  6. Check the Condit, on at while (i<=n) for a given value of i, if this condition is true then j initialized to 1.

  7. Inside this while loop use two more while loop and another if-else statement to print the pattern.

  8. If if(i==1 || i==n) condition is a) true , j=1, and inner while loop prints charter until the condition while(j <=n) is false. b)false, come to the else part then while loop prints charter at j=1 or j=n,until while(j<=n) condition is false, prints space at j!=1 and j!=n .

  9. Display the result.

  10. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program To Print Hollow Mirrored Rhombus
import java.util.Scanner;
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();	 
        System.out.println("Enter the Symbol : ");
        char ch = sc.next().charAt(0);
	    int i=1;
	    int j;
    	while(i<=n)
        {
            j=i;
            while(--j>0)
            {
               System.out.print(" ");
            }
	        if(i==1 || i==n)
            {
               j=1;
               while(j <=n)
               {
     	           System.out.print(ch);
     	           j++;
    	      }
   	        }
            else
	        {
            	j=1;
            	while(j<=n)
            	{
                    if(j==1 || j==n)
                  	System.out.print(ch);
                    else
                    System.out.print(" ");
		            j++;
               }
            }
            System.out.println();
            i++;
        }                   
    }
}


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

Program 3: Print the Hollow Mirrored Rhombus

In this program, we will see how to print the hollow mirrored rhombus star pattern 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 a do-while loop to print the pattern.

  6. For the given i, j do loop starts the execution, the inner do loop prints the one space then checks the condition while (–j>0), if it is true then it prints space until the condition while(–j>0) is false.

  7. Check the if condition. If it's true, then the inner do loop prints one charter and then checks the condition while(++j<=n), it prints until the condition while(++j<=n) is false.

  8. If it's false, then come to the else part and for j=1 the inner loop prints one character then checks the condition while (++j<=n), it is false so prints space, repeats until the condition ++j<=n is false.

  9. Display the result.

  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print Hollow Mirrored Rhombus
import java.util.Scanner;
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();	 
        System.out.println("Enter the Symbol : ");
        char ch = sc.next().charAt(0);
	    int i=1,j=1; 
        do 
        {
            j=i;
            do
            {
               System.out.print(" ");
            }
	        while(--j>0);
		    if(i==1 || i==n)
            {
               j=1;
               do
               {
     	          System.out.print(ch);
     		   }
			   while(++j<=n);
            }
            else
	    	{
                  j=1;
                  do
                  {
                        if(j==1 || j==n)
                        System.out.print(ch);
                        else
                        System.out.print(" ");
                   }while(++j<=n);
	        }
            System.out.println();
            ++i;
        }  while(i<=n);     
    }
}


Enter the number of rows: 8
Enter the Symbol: *
********
* *
* *
* *
* *
* *
* *
********



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.