Dark Mode On/Off

# Java Program to Print the Mirrored Right Triangle Pattern

In this tutorial, we will see how to print the mirrored right triangle pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the 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: 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: Print the Mirrored Right Triangle Pattern

In this program, we will see how to print the mirrored right triangle pattern 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 a for loop to print the pattern.
6. First run the outer for loop with the structure for(int i=1;i<=n;i++) to iterate through rows from i=1 to i=n.
7. The condition of outer loop is true then 1st inner loop runs with the structure for(int j=0;j<n-i;j++) and prints space if i<=n.
8. The second inner loop runs with the structure and prints character if j<i.
9. Then the cursor comes to the next line and 2nd iteration will begin, repeat until i<=n.
10. Display the result.
11. Stop

The below example illustrates the implementation of the above algorithm.

``````//Java Program to Print the Mirrored Right Triangle Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
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=1;i<=n;i++)
{
for(int j=0;j<n-i;j++)
{
System.out.print(" ");
}
for(int j=0;j<i;j++)
{
System.out.print(ch);
}
System.out.println();
}
}
}``````

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

## Program 2: Print the Mirrored Right Triangle Pattern

In this program, we will see how to print the mirrored right triangle pattern 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 a while loop to print the pattern.
6. First, check the condition at outer while loop i<=n if true initialize j to 0.
7. The first inner while loop prints space if j++<(n-i) is true, repeats until the condition fails.
8. j value initialized to 0, the second inner while loop prints character j<i is true, and j increased by 1, repeat until the condition is false.
9. The cursor comes to the next line, i value increased by 1 and again checks the outer while condition, repeat until the condition is false at the outer loop.
10. Display the result.
11. Stop

The below example illustrates the implementation of the above algorithm.

``````//Java Program to Print the Mirrored Right Triangle Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
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=1,j;
while(i<=n)
{
j=0;
while(j++<(n-i))
{
System.out.print(" ");
}
j=0;
while(j<i)
{
System.out.print(ch);
j++;
}
System.out.println();
i++;
}
}
}``````

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

## Program 3: Print the Mirrored Right Triangle Pattern

In this program, we will see how to print the mirrored right triangle pattern 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 a do-while loop to print the pattern.
6. First, initialize j to 0. The inner do loop prints space once then it checks the condition (++j<n-i) if it is true then it prints space, repeats until the condition is false.
7. Next, initialize j to 0. The second inner loop prints the character then checks the condition while(j++<i), if true it prints the character again checks the condition repeats until the condition fails.
8. The cursor comes to the next line, then checks the condition at the outer do-while loop while(++i<n) if it is true then the outer do loop executes the code, repeats until the condition at the outer do-while loop is false.
9. Display the result.
10. Stop

The below example illustrates the implementation of the above algorithm.

``````//Java Program to Print the Mirrored Right Triangle Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
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=1,j;
do
{
j=0;
do
{
System.out.print(" ");
}while(j++<(n-i));
j=0;
do
{
System.out.print(ch);
j++;
}while(j<i);
System.out.println();
i++;
}while(i<=n);
}
}``````

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