0
0
JavaProgramBeginner · 2 min read

Java Program to Print Right Triangle Pattern

Use nested for loops in Java where the outer loop controls the rows and the inner loop prints stars (*) up to the current row number, like for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }.
📋

Examples

Input3
Output* ** ***
Input5
Output* ** *** **** *****
Input1
Output*
🧠

How to Think About It

To print a right triangle pattern, think of printing one star on the first line, two stars on the second, and so on until the given number of lines. Use one loop to count the lines and a nested loop to print stars equal to the current line number.
📐

Algorithm

1
Get the number of rows (n) as input.
2
Start an outer loop from 1 to n for each row.
3
Inside the outer loop, start an inner loop from 1 to the current row number.
4
Print a star (*) in the inner loop without moving to a new line.
5
After the inner loop ends, print a new line to move to the next row.
6
Repeat until all rows are printed.
💻

Code

java
public class RightTrianglePattern {
    public static void main(String[] args) {
        int n = 5; // Number of rows
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Output
* ** *** **** *****
🔍

Dry Run

Let's trace the example with n=3 through the code

1

Start outer loop with i=1

Inner loop runs j=1 to 1, prints '*' once, then prints newline

2

Outer loop i=2

Inner loop runs j=1 to 2, prints '**', then newline

3

Outer loop i=3

Inner loop runs j=1 to 3, prints '***', then newline

i (row)j (stars printed)Output line
11*
22**
33***
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, each iteration representing one row of the triangle.

Step 2: Inner loop prints stars

The inner for loop runs from 1 to the current row number i, printing that many stars on the same line.

Step 3: New line after each row

After printing stars for a row, System.out.println() moves the cursor to the next line to start the next row.

🔄

Alternative Approaches

Using while loops
java
public class RightTrianglePattern {
    public static void main(String[] args) {
        int n = 5;
        int i = 1;
        while (i <= n) {
            int j = 1;
            while (j <= i) {
                System.out.print("*");
                j++;
            }
            System.out.println();
            i++;
        }
    }
}
This uses while loops instead of for loops; functionally the same but slightly more verbose.
Using String.repeat (Java 11+)
java
public class RightTrianglePattern {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            System.out.println("*".repeat(i));
        }
    }
}
This uses the built-in <code>String.repeat()</code> method to print stars, making code shorter and cleaner but requires Java 11 or newer.

Complexity: O(n^2) time, O(1) space

Time Complexity

The outer loop runs n times and the inner loop runs up to n times in the last iteration, resulting in roughly n*(n+1)/2 operations, which is O(n^2).

Space Complexity

The program uses a fixed amount of extra space regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

Using String.repeat() is concise and may be slightly faster due to optimized internal implementation, but all approaches have the same O(n^2) time complexity.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clear and classic approach
Nested while loopsO(n^2)O(1)Alternative loop style
String.repeat() methodO(n^2)O(1)Concise code, requires Java 11+
💡
Use nested loops where the inner loop prints stars equal to the current row number.
⚠️
Beginners often forget to print a new line after each row, causing all stars to print on one line.