Java Program to Print the Hollow Pyramid Star Pattern
In this tutorial, we will see how to print the hollow pyramid 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 pyramid 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:
*
* *
* *
* *
********
This can be done by using the following methods:
Approach 1: Using a For Loop
Approach 2: Using a While loop
Approach 3: Using a do-while loop
Let us look at each of these approaches for a better understanding.
Program 1: Java Program to Print the Hollow Pyramid Star Pattern
In this program, we will see how to print the hollow pyramid star pattern in java using a for loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use a for loop to print the pattern.
-
Use the outer for loop to iterate through rows from 1 to n.
-
Use the first inner for loop to iterate from 1 to n and then print the required space.
-
Inside the for use another for loop to print the required space.
-
Use an if-else statement to print the required pattern.
-
If the "if" condition is true then use a for loop that iterates from 1 to i*(2-1) and print the star symbol.
-
If the "if" condition is false, then the next for loop prints character if if(j==1 || j==i*2-1) condition is true. Otherwise, it prints space.
-
Display the pattern.
-
Stop.
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
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();
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<=i*2-1;j++)
{
System.out.print("*");
}
else
{
for(int j=1;j<=i*2-1;j++)
{
if(j==1 || j==i*2-1)
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.println();
}
}
}
Enter the number of rows: 8
*
* *
* *
* *
* *
* *
* *
***************
Program 2: Java Program to Print the Hollow Pyramid Star Pattern
In this program, we will see how to print the hollow pyramid star pattern in java using a while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use a while loop to print the pattern.
-
Use the outer while loop to iterate until i<=n.
-
Use the inner while loop to iterate through the columns.
-
Inside the outer while, use another if condition to print the required space.
-
If if(i==1 || i==n) condition true then the first inner loop will print character until the j++<=i*2-1 condition is true.
-
If condition is false then the second inner loop prints character if if(j==1 || j==(i*2)-1) is true, until while(j<=(i*2)-1) is false, otherwise prints space.
-
Display the pattern.
-
Stop.
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
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();
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++<=i*2-1)
{
System.out.print("*");
}
}
else
{
j=1;
while(j<=(i*2)-1)
{
if(j==1 || j==(i*2)-1)
System.out.print("*");
else
System.out.print(" ");
j++;
}
}
System.out.println();
i++;
}
}
}
Enter the number of rows: 7
*
* *
* *
* *
* *
* *
*************
Program 3: Java Program to Print the Hollow Pyramid Star Pattern
In this program, we will see how to print the hollow pyramid star pattern in java using a do-while loop.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a variable to store the number of rows.
-
Ask the user to initialize the variable.
-
Use a do-while loop to print the pattern.
-
The first inner do-while loop will iterate until the condition ++j <=n-i+1
-
Inside the outer do-while use another if condition to print the required space.
-
If the "if" condition true then the inner do-while loop prints charter until the condition (++j <=i*2-1) is false.
-
If the“if” condition is false then next do-while loop prints charter if if(j==1 || j==i*2-1) is true until while(++j<=i*2-1) is false, otherwise it prints space.
-
Display the pattern.
-
Stop.
The below example illustrates the implementation of the above algorithm.
//Java Program to Print the Hollow Pyramid Star Pattern
import java.util.*;
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();
int i=1;
int j;
do
{
j=1;
do
{
System.out.print(" ");
}while(++j <=n-i+1);
if(i==1 || i==n)
{
j=1;
do
{
System.out.print("*");
}while(++j <=i*2-1);
}
else
{
j=1;
do
{
if(j==1 || j==i*2-1)
System.out.print("*");
else
System.out.print(" ");
}while(++j<=i*2-1);
}
System.out.println();
++i;
} while(i<=n);
}
}
Enter the number of rows: 7
*
* *
* *
* *
* *
* *
*************