Java Program to Print Right Triangle Star Pattern

Last Updated : 23 Jan, 2026

A right triangle star pattern is a triangle made of asterisks * where the number of stars in each row increases from 1 up to n.

Example:

Input : n = 5
Output:
*
* *
* * *
* * * *
* * * * *

Method 1: Using Nested Loops (Iterative Approach)

This is the most common approach. The outer loop iterates over rows, and the inner loop prints stars in each row.

Java
public class RightTriangleStarPattern {
    public static void main(String[] args) {
        int rows = 5; 
        
        for (int i = 1; i <= rows; i++) {       // Outer loop for rows
            for (int j = 1; j <= i; j++) {     // Inner loop for stars
                System.out.print("* ");
            }
            System.out.println();               // Move to next line
        }
    }
}

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

Explanation:

  • The outer loop runs from 1 to rows and controls the number of rows printed.
  • The inner loop runs from 1 to i, printing one * for each column in the current row.
  • After each row is printed, println() moves the cursor to the next line.

Method 2: Using Recursion

We can also print the triangle recursively by printing stars row by row.

Java
class RightTriangleRecursive {

    // Prints stars in a single row
    public static void printStars(int stars) {
        if (stars == 0) return;
        System.out.print("* ");
        printStars(stars - 1);
    }
    // Prints rows recursively
    public static void printTriangle(int currentRow, int totalRows) {
        if (currentRow > totalRows) return;
        printStars(currentRow);
        System.out.println();
        printTriangle(currentRow + 1, totalRows);
    }
    public static void main(String[] args) {
        int n = 5;
        printTriangle(1, n);
    }
}

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

Explanation:

  • The printStars() method prints stars in a single row using recursion until the count becomes zero.
  • The printTriangle() method recursively prints each row by increasing the current row count.
  • Recursion terminates when the current row exceeds the total number of rows.

Method 3: Using a Single Loop with String Concatenation

This method builds each row using string concatenation.
 

Java
public class RightTriangleString {
    public static void main(String[] args) {
        int n = 5;
        String row = "*";

        for (int i = 1; i <= n; i++) {
            System.out.println(row);
            row += "*"; // Add one more star for next row
        }
    }
}

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

Explanation:

  • A string variable row is initialized with one *.
  • In each iteration, the current value of row is printed.
  • One additional * is concatenated to the string for the next row.

Method 4: Using a Single Loop with numStars Counter

This approach keeps a counter for stars per row.

Java
public class RightTriangleCounter {
    public static void main(String[] args) {
        int n = 5;
        int numStars = 1;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= numStars; j++) {
                System.out.print("* ");
            }
            numStars++;
            System.out.println();
        }
    }
}

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

Explanation:

  • The variable numStars keeps track of how many stars should be printed in each row.
  • The inner loop prints stars equal to the value of numStars.
  • After printing each row, numStars is incremented to increase stars for the next row.
Comment
Article Tags: