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:

  1. Upper half: From 1 to n rows – print decreasing spaces and increasing stars.
  2. 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();

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...