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

Java Program to Print the Hollow Inverted Pyramid Star Pattern

In this tutorial, we will see how to print the hollow inverted 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 inverted 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: 5

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 Inverted Pyramid Star Pattern

In this program, we will see how to print the hollow inverted 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 for loops to print the pattern.

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

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

  8. If the first inner loop condition is false then check the” if” condition.

  9. If the condition is true then the first loop prints character for ( i==1 or i==n) and j<=i*2-1.

  10. If the condition is false then the second loop prints character for (j==1 || j==i*2-1), prints space for (j!=1&j!=i*2-1) until j<=i*2-1

  11. Display the result.

  12. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Inverted 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=n;i>0;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: 6
***********
* *
* *
* *
* *
*

Program 2: Java Program to Print the Hollow Inverted Pyramid Star Pattern

In this program, we will see how to print the hollow inverted 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 while loops to print the pattern.

  6. Initialize the outer loop variable to the number of rows.

  7. The outer while loop will iterate till i>0

  8. Initialize the inner loop variable to 1.

  9. Use another while loop that will iterate till ++j<=i to print the space.

  10. Now, use an if-else statement to print the required pattern.

  11. If the condition is true first while loop prints character until j++<=i*2-1 is false.

  12. If condition false next while loop prints character for j==1 , j==(i*2)-1), until while(j<=(i*2)-1) condition is false.

  13. And print space for j!=1 and J!=(i*2)-1, until while(j<=(i*2)-1) condition is false.

  14. Display the result.

  15. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Inverted 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=n;
        int j;
        while(i>0)
        {
           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 Inverted Pyramid Star Pattern

In this program, we will see how to print the hollow inverted 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 do-while loops to print the pattern.

  6. Initialize the outer loop variable to the number of rows.

  7. The outer do-while loop will iterate till i>0

  8. Initialize the inner loop variable to 1.

  9. Use another do-while loop that will iterate till ++j<=n-i+1 and print the space.

  10. Now, use an if-else statement to print the required pattern.

  11. If the condition is true first do-while loop prints character until j++<=i*2-1 is false.

  12. If condition false next do-while loop prints character for j==1 , j==(i*2)-1), until while(j<=(i*2)-1) condition is false.

  13. And print space for j!=1 and j!=(i*2)-1, until while(j<=(i*2)-1) condition is false.

  14. Display the result.

  15. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Hollow Inverted 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=n;
        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>0); 
    }
}


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