Java Program to Print Mirror Upper Star Triangle Pattern

In the world of programming, one of the fundamental exercises that help develop logical thinking and problem-solving skills is the creation of various patterns using loops. Patterns not only make coding fun but also improve our understanding of how loops and conditions work in programming languages. One such interesting pattern is the "Mirror Upper Star Triangle Pattern."

Understanding the Mirror Upper Star Triangle Pattern

Before diving into the code, let's visualize what a Mirror Upper Star Triangle Pattern looks like. This pattern is essentially a right-aligned triangle composed of stars (*). For a given number of rows, the pattern looks like this:

For n = 5 (number of rows)

In this pattern:

  • The number of rows is n.
  • The first row has n-1 spaces followed by 1 star.
  • The second row has n-2 spaces followed by 2 stars, and so on.
  • The nth row has 0 spaces followed by n stars.

Steps to Create Star Triangle Pattern

  1. Read the Number of Rows: Input the number of rows from the user.
  2. Outer Loop for Rows: Iterate over each row.
  3. Inner Loop for Spaces: For each row, print the required number of spaces.
  4. Inner Loop for Stars: After the spaces, print the required number of stars.

Implementation

Let's break down the Java code step-by-step to generate the Mirror Upper Star Triangle Pattern.

Java Program to Print Mirror Upper Star Triangle Pattern

Create a new Java class, for example, MirrorUpperStarTriangle. Here's the complete code for generating the pattern:

MirrorUpperStarTriangle.java

Output:

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

Java Program to Print Mirror Lower Star Triangle Pattern

To create an inverted mirror upper star triangle, the pattern would look like this for n = 5:

LowerStarTrianglePattern.java

Output:

* * * * * * * 
 * * * * * * 
   * * * * * 
   * * * * 
    * * * 
     * * 
      * 

Java Program to Print Upper and Lower Star Triangle Pattern Combinedly

CombinedStarTrianglePattern.java

Output:

Java Program to Print Mirror Upper Star Triangle Pattern

Creating patterns like the Mirror Upper Star Triangle in Java is an excellent way to improve our understanding of loops and conditional statements. This exercise not only sharpens our coding skills but also enhances our ability to visualize and solve problems programmatically. By experimenting with different patterns and variations, we can further develop logical thinking and become more proficient in Java programming.