Print Upper Star Triangle Pattern in Java



In this article, we will learn to print the upper star triangle pattern in Java. Printing patterns is a common exercise to strengthen the understanding of loops in programming. One of the most popular patterns is the Upper Star Triangle, which visually resembles an inverted right-angled triangle of stars aligned from the top.

Below is a demonstration of the same ?

Input

Enter the number of rows : 8

Output

The upper star triangle star pattern :
              *
            * *
          * * *
        * * * *
      * * * * *
    * * * * * *
  * * * * * * *
* * * * * * * *

Using Nested Loops

The first approach involves using two nested for loops. The outer loop controls the rows, while the inner loops control the spaces and stars in each row.

Algorithm

Following are the steps to print the upper star triangle pattern ?

  • Step 1 - START
  • Step 2 - Declare three integer values namely i, j and my_input
  • Step 3 - Read the required values from the user/ define the values
  • Step 4 - We iterate through two nested 'for' loops to get space between the characters.
  • Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character.
  • Step 6 - Now, print a newline to get the specific number of characters in the subsequent lines.
  • Step 7 - Display the result
  • Step 8 - Stop
for (i=0; i<my_input; i++){
         for (j=2*(my_input-i); j>=0; j--){}}

Example

Below is an example of printing an upper star triangle pattern using nested loops ?

public class UpperStarTriangle{
   public static void main(String args[]){
      int i, j, my_input;
      my_input = 8;
      System.out.println("The number of rows is defined as " +my_input);
      System.out.println("The upper star triangle star pattern: ");
      for (i=0; i<my_input; i++){
         for (j=2*(my_input-i); j>=0; j--){
            System.out.print(" ");
         }
         for (j=0; j<=i; j++ ){
             System.out.print("* ");
        }
        System.out.println();
      }
   }
}

Output

The number of rows is defined as 8
The upper star triangle star pattern: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Time Complexity: O(n²) (due to nested loops for spaces and stars).

Space Complexity: O(1) (no extra space except variables).

Using String Manipulation

The second approach simplifies the logic by generating strings for spaces and stars directly. This avoids complex calculations for spaces.

Using String.repeat() ?

  • Spaces and stars are calculated using the repeat() method to generate the desired number of characters dynamically.
  • " ".repeat(2 * (my_input - i)) generates the required spaces for alignment.
  • "* ".repeat(i + 1) generates the stars for the current row
String spaces = " ".repeat(2 * (my_input - i)); // Adjust spacing
String stars = "* ".repeat(i + 1); // Increase stars with each row

Example

Below is an example of printing an upper star triangle pattern using string manipulation ?

public class UpperStarTriangle {
    public static void main(String[] args) {
        int my_input = 8; // Number of rows for the triangle
        
        System.out.println("The number of rows is defined as " + my_input);
        System.out.println("The upper star triangle pattern: ");
        
        for (int i = 0; i < my_input; i++) {
            // Generate spaces and stars using string manipulation
            String spaces = " ".repeat(2 * (my_input - i)); // Adjust spacing
            String stars = "* ".repeat(i + 1); // Increase stars with each row
            
            // Print the row
            System.out.println(spaces + stars);
        }
    }
}

Output

The number of rows is defined as 8
The upper star triangle star pattern: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Time Complexity: O(n²) (because String.repeat() internally iterates).

Space Complexity: O(n) (extra space for strings created in each iteration).

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-01-07T18:56:02+05:30

888 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements