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:
-
Start
-
Create an instance of the Scanner class.
-
Declare variables to store the number of rows and the pattern symbol.
-
Ask the user to initialize these variables.
-
Use three for loops to print the pattern.
-
The outer for loop will iterate through the rows.
-
The first inner for loop is used to print the required space.
-
Use an if-else statement to check for the conditions.
-
The second inner for loop prints the required pattern symbol.
-
Display the result.
-
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:
-
Start
-
Create an instance of the Scanner class.
-
Declare variables to store the number of rows and the pattern symbol.
-
Ask the user to initialize these variables.
-
Use three while loops to print the pattern.
-
The outer while loop will iterate through the rows.
-
The first while for loop is used to print the required space.
-
Use an if-else statement to check for the conditions.
-
The second inner while loop prints the required pattern symbol.
-
Display the result.
-
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:
-
Start
-
Create an instance of the Scanner class.
-
Declare variables to store the number of rows and the pattern symbol.
-
Ask the user to initialize these variables.
-
Use three do-while loops to print the pattern.
-
The outer do-while loop will iterate through the rows.
-
The first do-while for loop is used to print the required space.
-
Use an if-else statement to check for the conditions.
-
The second inner do-while loop prints the required pattern symbol.
-
Display the result.
-
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: *
********
* *
* *
* *
* *
* *
**
*