PUBLISHED ON: APRIL 17, 2021
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:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the number of rows and the pattern symbol.
- Ask the user to initialize these variables.
- Use a for loop to print the pattern.
- Inside this for loop use two more for loop to print the required pattern.
- Now use the first for loop to print the space.
- Use the second for loop to print the pattern.
- Display the result.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the number of rows and the pattern symbol.
- Ask the user to initialize these variables.
- Use a while loop to print the pattern.
- Inside this while loop, use two more while loops to print the required pattern.
- Now use the first while loop to print the space.
- Use the second while loop to print the pattern.
- Display the result.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare variables to store the number of rows and the pattern symbol.
- Ask the user to initialize these variables.
- Use a do-while loop to print the pattern.
- Inside this do-while loop use two more do-while loops to print the required pattern.
- Now use the first do-while loop to print the space.
- Use the second do-while loop to print the pattern.
- Display the result.
- 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
*********
*********
*********
*********
*********
*********
*********
*********