Java Program to Print Mirror Upper Star Triangle Pattern

Last Updated : 7 Aug, 2026

The Upper Star Triangle Pattern is a simple Java pattern-printing program in which each row contains one more star than the previous row. It is commonly used to practice nested loops, spacing, and pattern generation.

  • Leading spaces decrease as the row number increases.
  • Useful for learning nested loops and pattern logic.

Illustration

Input: Rows = 5

Output: *
**
***
****
*****

Approach

  • Initialize the number of rows.
  • Use an outer loop to print each row.
  • Print the required leading spaces.
  • Print stars equal to the current row number.
  • Move to the next line.

ava Program to Print Mirror Upper Star Triangle Pattern

Example: Upper Part

Java
// Importing input output classes
import java.io.*;

// Main Class
class GFG {

    // Method 1
    // To print upper part of the pattern
    private static void displayUpperPart(int size)
    {

        // Declaring variables for rows and columns
        // respectively
        int m, n;

        // Outer loop for rows
        for (m = size - 1; m >= 0; m--) {

            // Inner loop 1
            // to print triangle 1
            for (n = 0; n < m; n++) {

                // Printing whitespace
                System.out.print(" ");
            }

            // Inner loop 2
            // to print triangle 2
            for (n = m; n <= size - 1; n++) {

                // Printing star with whitespace
                System.out.print("*"
                                 + " ");
            }

            // By now done with one row so next line
            System.out.println();
        }
    }

    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables to
        // size of the triangle
        int size = 7;

        // Calling the above Method 1 to
        // print and display the upper part of triangle
        displayUpperPart(size);
    }
}

Output
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

Explanation: The outer loop controls the number of rows. The first inner loop prints the leading spaces, while the second inner loop prints stars equal to the current row number, forming an upper star triangle.

Example: Lower Part of the triangle 

Java
// Importing input output classes
import java.io.*;

// Main Class
class GFG {

    // Method 1
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {

        // Declaring variables for rows and columns
        // respectively
        int m, n;

        // Outer loop fo Rows
        for (m = 1; m <= size; m++) {

            // Inner loop 1 to print triangle 3
            for (n = 1; n < m; n++) {

                // Printing whitespace
                System.out.print(" ");
            }

            // Inner loop 2 to print triangle 4
            for (n = m; n <= size; n++) {

                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }

            // By now done with one row so new line
            System.out.println();
        }
    }

    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        // This is number of rows
        int size = 7;

        // Calling the above Method1
        // to display lower part of the triangle
        displayLowerPart(size);
    }
}

Output
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 

 Example: Complete Mirror Upper Star Triangle Pattern

Java
// Importing input output classes
import java.io.*;

// Main Class
public class GFG {

    // Method 1
    // To print upper part of the pattern
    private static void displayUpperPart(int size)
    {

        // Declaring variables for rows and columns
        // respectively
        int m, n;

        // Outer loop for rows
        for (m = size - 1; m >= 0; m--) {

            // Inner loop 1
            for (n = 0; n < m; n++) {

                // Printing whitespace
                System.out.print(" ");
            }

            // Inner loop 2
            for (n = m; n <= size - 1; n++) {

                // Printing star with whitespace
                System.out.print("*"
                                 + " ");
            }

            // By now we are done with one row so new line
            System.out.println();
        }
    }

    // Method 2
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {

        // Declaring variables for rows and columns
        // respectively
        int m, n;

        // Outer loop for rows
        for (m = 1; m <= size; m++) {

            // Inner loop 1
            for (n = 1; n < m; n++) {

                // Printing whitespace
                System.out.print(" ");
            }

            // Inner loop 2
            for (n = m; n <= size; n++) {

                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }

            // By now we are done with one row so new line
            System.out.println();
        }
    }

    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        int size = 7;

        // Calling Method 1 to
        // display the upper part
        displayUpperPart(size);

        // Calling Method 2 to
        // display lower part
        displayLowerPart(size);
    }
}

Output
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 
Comment