- 1Print Right Triangle Star Pattern in Java
- 2Print Inverted Right Triangle Star Pattern
- 3Print Mirrored Right Triangle Star Pattern in Java
- 4Print Inverted Mirrored Right Triangle Star Pattern
- 5Print Right Pascal's Triangle Star Pattern in Java
- 6Print K Shape Star Pattern in Java
- 7Print Mirrored Right Pascal's Triangle Pattern in Java
- 8Print Full Reverse K Star Pattern in Java
- 9Print Pyramid Star Pattern in Java
- 10Print Inverted Pyramid Star Pattern in Java
- 11Print Diamond Star Pattern in Java
- 12Print Hollow Diamond Star Pattern in Java
- 13Print Sandglass Star Pattern in Java
- 14Print Butterfly Star Pattern in Java
Print Inverted Mirrored Right Triangle Star Pattern
Topic
In this program, you'll learn how to print an inverted mirrored right triangle star pattern using nested loops in Java. This is a classic pattern question asked in many interviews to test your understanding of loop control and output formatting.
An inverted mirrored right triangle starts with all stars aligned to the right in the first row and gradually reduces the number of stars in each next row, moving diagonally to the bottom-left.
Examples
Input: n = 5
*****
****
***
**
*
Input: n = 3
***
**
*
Edge Case (n = 1):
*
Interviewer Expectations
The interviewer wants to evaluate your:
- Understanding of nested loops
- Ability to control spaces and stars precisely
- Attention to detail in formatting output
Approach
We will use two nested loops. The outer loop runs from 0 to n-1 for each row. In each row:
- First, print spaces equal to the current row index (i.e., i spaces).
- Then, print stars equal to (n - i).
Dry Run for n = 4
i = 0 → spaces: 0, stars: 4 → ****
i = 1 → spaces: 1, stars: 3 → ***
i = 2 → spaces: 2, stars: 2 → **
i = 3 → spaces: 3, stars: 1 → *
Java Program
public class PatternPrinter {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int space = 0; space < i; space++) {
System.out.print(" ");
}
for (int star = 0; star < n - i; star++) {
System.out.print("*");
}
System.out.println();
}
}
}
*****
****
***
**
*
Possible Followup Questions with Answers
1. How would you print a normal (non-inverted) mirrored right triangle?
Just reverse the order of printing spaces and stars:
for (int i = 0; i < n; i++) {
for (int space = 0; space < n - i - 1; space++) {
System.out.print(" ");
}
for (int star = 0; star <= i; star++) {
System.out.print("*");
}
System.out.println();
}
2. How can you print the pattern using a function?
You can create a separate function and pass n
as a parameter:
void printInvertedMirroredTriangle(int n) {
for (int i = 0; i < n; i++) {
for (int space = 0; space < i; space++) {
System.out.print(" ");
}
for (int star = 0; star < n - i; star++) {
System.out.print("*");
}
System.out.println();
}
}
3. What happens if you input n = 0?
The program won’t print anything, as the loop condition i < n
will fail immediately.
Next Topic ⮕Print Right Pascal's Triangle Star Pattern in Java
Comments
Loading comments...