Signup/Sign In

Java Program to print Rhombus Star Pattern

In this tutorial, we will see how to print the 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 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 Rhombus star pattern

In this program, we will see how to print the rhombus 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 a for loop to print the pattern.
  6. Inside this for loop use two more for loop to print the required pattern.
  7. Now use the first for loop to print the space.
  8. Use the second for loop to print the pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Rhombus 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<=n;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }             
    }
}


Enter the number of rows: 5
*****
*****
*****
*****
*****

Program 2: Print the Rhombus Star Pattern

In this program, we will see how to print the 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 two more while loops to print the required pattern.
  7. Now use the first while loop to print the space.
  8. Use the second while loop to print the pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Rhombus 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;
 	    while(i<=n)
        {
            int j=1;
            while(j++<=n-i)
            {
                System.out.print(" ");
            }
            j=1;
            while(j++<=n)
            {
                System.out.print("*");
            }
            System.out.println();
		    i++;
       }         
    }
}


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

Program 3: Print the Rhombus Star Pattern

In this program, we will see how to print the rhombus 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 a do-while loop to print the pattern.
  6. Inside this do-while loop use two more do-while loops to print the required pattern.
  7. Now use the first do-while loop to print the space.
  8. Use the second do-while loop to print the pattern.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Rhombus 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;
 	    do
        {
            int j=1;
            do
            {
                System.out.print(" ");
            }while(j++<=n-i);
            j=1;
            do
            {
                System.out.print("*");
            }while(j++<=n);
            System.out.println();
		    i++;
       } while(i<=n);        
    }
}


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



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.