Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

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:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a for loop to print the pattern.

  6. Use the outer for loop to iterate through rows from 1 to n.

  7. Use the first inner for loop to iterate from 1 to n and then print the required space.

  8. Inside the for use another for loop to print the required space.

  9. Use an if-else statement to print the required pattern.

  10. If the "if" condition is true then use a for loop that iterates from 1 to i*(2-1) and print the star symbol.

  11. 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.

  12. Display the pattern.

  13. 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:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a while loop to print the pattern.

  6. Use the outer while loop to iterate until i<=n.

  7. Use the inner while loop to iterate through the columns.

  8. Inside the outer while, use another if condition to print the required space.

  9. 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.

  10. 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.

  11. Display the pattern.

  12. 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:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare a variable to store the number of rows.

  4. Ask the user to initialize the variable.

  5. Use a do-while loop to print the pattern.

  6. The first inner do-while loop will iterate until the condition ++j <=n-i+1

  7. Inside the outer do-while use another if condition to print the required space.

  8. If the "if" condition true then the inner do-while loop prints charter until the condition (++j <=i*2-1) is false.

  9. 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.

  10. Display the pattern.

  11. 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
*
* *
* *
* *
* *
* *
*************