- 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 Mirrored Right Pascal's Triangle Pattern in Java
Topic: Print Mirrored Right Pascal's Triangle Pattern
The mirrored right Pascal's triangle is a symmetrical star pattern that consists of two right-angled triangles joined together at the base. It's commonly asked in interviews to test your understanding of nested loops and spaces. As a beginner, this is a great program to practice loops and formatting output in Java.
Examples
Input: n = 5
Output:
*
**
***
****
*****
****
***
**
*
Input: n = 3
Output:
*
**
***
**
*
In both examples, the number of rows is 2 * n - 1
. The upper triangle increases in stars, and the lower triangle decreases, with appropriate leading spaces to create the mirrored effect.
Interviewer Expectations
The interviewer expects you to:
- Understand nested loops for controlling rows and columns.
- Manage spacing correctly to align the stars.
- Handle symmetrical patterns by splitting the logic into two parts (increasing and decreasing).
- Write clean, structured, and readable code.
Approach
We break the pattern into two parts:
- Upper half: From 1 to n rows – print decreasing spaces and increasing stars.
- Lower half: From n-1 to 1 rows – print increasing spaces and decreasing stars.
Dry Run for n = 3
i = 1 → spaces = 2, stars = 1 → *
i = 2 → spaces = 1, stars = 2 → **
i = 3 → spaces = 0, stars = 3 → ***
i = 2 → spaces = 1, stars = 2 → **
i = 1 → spaces = 2, stars = 1 → *
Java Program
public class MirroredRightPascalsTriangle {
public static void main(String[] args) {
int n = 5; // Height of the triangle
// Upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
// Lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
**
***
****
*****
****
***
**
*
Possible Followup Questions with Answers
Q1. Can you modify this to print the same pattern using numbers instead of stars?
Yes. Just replace System.out.print("*");
with System.out.print(k);
or System.out.print(i);
depending on the desired pattern.
Q2. How would you print a right Pascal’s triangle without mirroring?
Omit the leading spaces in both halves. Just print increasing and then decreasing stars directly aligned to the left.
Q3. What if the user inputs the height instead of hardcoding n
?
Use a Scanner
object to read input from the user:
Scanner sc = new Scanner(System.in);
System.out.print("Enter height: ");
int n = sc.nextInt();
Next Topic ⮕Print Full Reverse K Star Pattern in Java
Comments
Loading comments...