Signup/Sign In
PUBLISHED ON: APRIL 17, 2021

Java Program to Print Inverted Star Pattern

In this tutorial, we will see how to print the inverted star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the inverted star patterns. 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: Print the Inverted Star Pattern

In this program, we will see how to print the inverted 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. To iterate through rows run the outer loop with the structure for(int i=n;i>0;iā€“).
  7. To iterate through rows run the inner loops.
  8. The first inner loop prints space if i>0 && j<n-i.
  9. The second inner loop prints character if i>0&&j<(i*2)-1
  10. Display the result.
  11. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print Inverted 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();	 
        for(int i=n;i>0 ;i--)
        {
	        for(int j=0;j<n-i;j++)
            {
                System.out.print(" ");
            }
            for(int j=0;j<(i*2)-1;j++)
            {
                System.out.print("*");
            }
           System.out.println();
        }  
    }
}


Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*

Program 2: Print the Inverted Star Pattern

In this program, we will see how to print the inverted 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. Outer while loop iterate until i>0 is false.
  7. The first inner while loop prints space if the condition j++<n-i is true.
  8. The second inner loop prints character if the condition j++<(i*2)-1 is true.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print the Inverted 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();	 
        int  i=n,j;
	    while(i>0)
        {
            j=0;
            while(j++<n-i)
            {
                System.out.print(" "); 
            }
	        j=0;
            while(j++<(i*2)-1)
            {
                System.out.print("*"); 
            }
            System.out.println();
            i--;
       }  
    }
}


Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*

Program 3: Print the Inverted Star Pattern

In this program, we will see how to print the inverted 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 outer do-while loop iterates until while(ā€“i>0) is false.
  7. The first inner loop prints space until the condition j++<n-i is false.
  8. The second inner loop prints the character until the condition j++<i*2-2 is false.
  9. Display the result.
  10. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program To Print the Inverted Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
        //Create  an instance of the Scanner Class 
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        int  i=n,j;
	    do
        {
	        j=0;
            do
            {
                System.out.print(" ");
            }while(j++<n-i);
            j=0;   
            do
            {
                System.out.print("*");
            }while(j++<i*2-2);
            System.out.println();
        }while(--i>0);       
    }
}


Enter the number of rows: 7
*************
***********
*********
*******
*****
***
*



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.