PUBLISHED ON: APRIL 17, 2021
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:
- 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 another for loop to print the spaces.
- Now use an if-else statement to print the pattern.
- Inside the if-else statement again use a for loop to check for the conditions.
- Display the result.
- 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:
- 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 another while loop to print the spaces.
- Now use an if-else statement to print the pattern.
- Inside the if-else statement again use a while loop to check for the conditions.
- Display the result.
- 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: *
*******
* *
* *
* *
* *
* *
*******