Signup/Sign In
PUBLISHED ON: APRIL 22, 2021

Java Program to print the Hollow Inverted Mirrored Right Triangle Star Pattern

In this tutorial, we will see how to print the hollow inverted mirrored right triangle pattern in java. First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the hollow 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 Hollow Inverted Mirrored Right Triangle Star Pattern

In this program, we will see how to print the hollow 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. Use an if-else statement to check for the conditions.

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

  10. Display the result.

  11. Stop.

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

//Java Program to print the Hollow 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(" ");
            }
            if(i==1 || i==n)
            for(int j=1;j<=i;j++)
            {
                System.out.print(ch);
            }
            else
            {
                for(int j=1;j<=i;j++)
                {  
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}


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

Program 2: Java Program to print the Hollow Inverted Mirrored Right Triangle Star Pattern

In this program, we will see how to print the hollow 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 while for loop is used to print the required space.

  8. Use an if-else statement to check for the conditions.

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

  10. Display the result.

  11. Stop.

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

//Java Program to print the Hollow 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;
        int j; 
        while(i>0)
        {
            j=n-i;
            while(j-->0)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            {
                j=1;
                while(j <=i)
                {
                    System.out.print(ch);
                    j++;
                }
            }
            else
            {
                j=1;
                while(j<=i)
                {
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                    j++;
                }
            }
            System.out.println();
            i--; 
        } 
    }
}


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

Program 3: Java Program to print the Hollow Inverted Mirrored Right Triangle Star Pattern

In this program, we will see how to print the hollow 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 do-while for loop is used to print the required space.

  8. Use an if-else statement to check for the conditions.

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

  10. Display the result.

  11. Stop.

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

//Java Program to print the Hollow 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;
        int j; 
        do 
        {
            j=n-i;
            while(j-->0)
            {
                System.out.print(" ");
            }
            if(i==1 || i==n)
            {
                j=1;
                do
                {
                    System.out.print(ch);
                }while(++j <=i);
            }
            else
            {
                j=1;
                do
                {
                    if(j==1 || j==i)
                    System.out.print(ch);
                    else
                    System.out.print(" ");
                }while(++j<=i);
            }
            System.out.println();
        } while(--i>0); 
    }
}


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



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.