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

Java Program to Print Inverted Mirrored Right Triangle

In this tutorial, we will see how to print the inverted mirrored right triangle in java First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the inverted mirrored right triangle 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: 6

Output:

******

*****

****

***

**

*

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 Mirrored Right Triangle

In this program, we will see how to print the inverted mirrored right triangle in java using a for loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows and the pattern symbol.

  4. Ask the user to initialize these variables.

  5. Use three for loops to print the pattern.

  6. The outer for loop will iterate through the rows.

  7. The first inner for loop is used to print the required space.

  8. The second inner for loop prints the required pattern symbol.

  9. Display the result.

  10. Stop.

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to print the Inverted Mirrored Right Triangle
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        for(int i=n;i>0;i--)
        {
            for(int j=n-i;j>0;j--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<i;j++)
            {
                System.out.print(ch);
            }
            System.out.println();
        }
    }
}


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

Program 2: Java Program to print the Inverted Mirrored Right Triangle

In this program, we will see how to print the inverted mirrored right triangle in java using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows and the pattern symbol.

  4. Ask the user to initialize these variables.

  5. Use three while loops to print the pattern.

  6. The outer while loop will iterate through the rows.

  7. The first inner while loop is used to print the required space.

  8. The second inner while loop prints the required pattern symbol.

  9. Display the result.

  10. Stop.

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to print the Inverted Mirrored Right Triangle
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        int i=n,j;
        while(i>0)
        {
            j=n-i;
            while(j-->0) 
            {
               System.out.print(" ");
            } 
            j=0;
            while(j++<i)
            {
               System.out.print(ch);
            }
            System.out.println();
            i--;
        } 
    }
}


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

Program 3: Java Program to print the Inverted Mirrored Right Triangle

In this program, we will see how to print the inverted mirrored right triangle in java using a do-while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows and the pattern symbol.

  4. Ask the user to initialize these variables.

  5. Use three do-while loops to print the pattern.

  6. The outer do-while loop will iterate through the rows.

  7. The first inner while loop is used to print the required space.

  8. The second inner do-while loop prints the required pattern symbol.

  9. Display the result.

  10. Stop.

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to print the Inverted Mirrored Right Triangle
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(); 
        System.out.println("Enter the symbol: ");
        char ch = sc.next().charAt(0);
        int i=n,j;
        do
        {
	        j=n-i;
            while(j-->0)
            {
                System.out.print(" ");
            } 
   	        j=0;                  
            do
            {
                System.out.print(ch);
            }while(++j<i);
            System.out.println();
        }while(--i>0);  
    }
}


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