Signup/Sign In

Java Program to Print the Inverted Right Triangle Star Pattern

In this tutorial, we will see how to print the inverted right triangle 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 right triangle 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 Inverted Right Triangle Star Pattern

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

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

  7. Use the inner for loop to iterate through columns from 0 to i.

  8. Print the pattern where the condition satisfies.

  9. Display the result.

  10. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Inverted Right Triangle 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=0;j<i;j++)
           {
               System.out.print("*");
           }
           System.out.println();
        }
     }
}


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

Program 2: Java Program to Print the Inverted Right Triangle Star Pattern

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

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

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

  8. Initialize the loop variable to 0.

  9. The inner while loop will iterate till j++<i.

  10. Print the pattern where the condition satisfies.

  11. Display the result.

  12. Stop.

The below example illustrates the implementation of the above algorithm.

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


Enter the number of rows: 6
******
*****
****
***
**
*

Program 3: Java Program to Print the Inverted Right Triangle Star Pattern

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

  6. Initialize the first 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 0.

  9. The inner while loop will iterate till ++j<i.

  10. Print the pattern where the condition satisfies.

  11. Display the result.

  12. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Inverted Right Triangle 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;
        do
        {
           int j=0;
           do
           {
               System.out.print("*");
           }while(++j<i);
           System.out.println();
        } while(--i>0) ;  
    }
}


Enter the number of rows: 6
******
*****
****
***
**
*



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.