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:
-
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.
-
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 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:
-
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 inner while loop is used to print the required space.
-
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 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:
-
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 inner while loop is used to print the required space.
-
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 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: *
********
*******
******
*****
****
***
**
*