Signup/Sign In

Java Program to Print the Hollow Rhombus Star Pattern

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

* * * *

* *

* *

* * * *

Program 1: Print the Hollow Rhombus Star Pattern

In this program, we will see how to print the hollow rhombus star pattern in Java 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 another for loop to print the spaces.
  7. Now use an if-else statement to print the pattern.
  8. Inside the if-else statement again use a for loop to check for the conditions.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the Hollow Rhombus Star Pattern
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 Symbol: ");
    System.out.println("");
    char c = sc.next().charAt(0);
	for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n-i;j++)
        {
           System.out.print(" ");
        }
	     if(i==1 || i==n)
        for(int j=1;j<=n;j++)
        {
           System.out.print(c);
        }
        else
	    {
           for(int j=1;j<=n;j++)
	       {  
         		if(j==1 || j==n)
                  System.out.print(c);
              	else
                  System.out.print(" ");
            }
         }
         System.out.println();
       }             
    }
}


Enter the number of rows: 7
Enter Symbol: *

*******
* *
* *
* *
* *
* *
*******

Program 2: Print the Hollow Rhombus Star Pattern

In this program, we will see how to print the hollow rhombus 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 a while loop to print the pattern.
  6. Inside this while loop use another while loop to print the spaces.
  7. Now use an if-else statement to print the pattern.
  8. Inside the if-else statement again use a while loop to check for the conditions.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to print the Hollow Rhombus Star Pattern
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 Symbol: ");
       System.out.println("");
       char c = sc.next().charAt(0);
	   int i=1;
 	   int j;
       while(i<=n)
       {
           j=1;
           while(j++<=n-i)
           {
                System.out.print(" ");
           }
		   if(i==1 || i==n)
		   {
             	j=1;
                while(j <=n)
                {
     	           System.out.print(c);
     		       j++;
    		    }
   		    }
  	       else
	       {
                j=1;
                while(j<=n)
                {
                    if(j==1 || j==n)
                        System.out.print(c);
                    else
                        System.out.print(" ");
			        j++;
                }
           }
           System.out.println();
          i++;
       }       
    }
}


Enter the number of rows: 7
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.