0
0
JavaProgramBeginner · 2 min read

Java Program to Print Star Pattern

You can print a star pattern in Java using nested for loops; for example, for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } prints a right-angled triangle of stars.
📋

Examples

Inputn = 1
Output*
Inputn = 4
Output* ** *** ****
Inputn = 0
Output
🧠

How to Think About It

To print a star pattern, think of rows and columns. For each row from 1 to n, print stars equal to the row number. Use one loop for rows and a nested loop for stars in each row.
📐

Algorithm

1
Get the number of rows n from the user or set it.
2
Start a loop from 1 to n for each row.
3
Inside this loop, start another 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 StarPattern {
    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 star pattern printing for n = 3 through the code

1

Initialize n

n = 3

2

First outer loop iteration (i=1)

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

3

Second outer loop iteration (i=2)

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

4

Third outer loop iteration (i=3)

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

i (row)Stars printed
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls rows

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

Step 2: Inner loop prints stars

The inner for loop runs from 1 to the current row number, printing 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.

🔄

Alternative Approaches

Using while loops
java
public class StarPattern {
    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++;
        }
    }
}
Uses while loops instead of for loops; slightly longer but same logic.
Using String.repeat (Java 11+)
java
public class StarPattern {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            System.out.println("*".repeat(i));
        }
    }
}
Simplifies inner loop by repeating star character; requires Java 11 or newer.

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

Time Complexity

The nested loops cause the program to print stars in a triangular pattern, resulting in about 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 can be more concise but internally still runs in O(n^2) time; all approaches have similar performance for this simple pattern.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Clear and beginner-friendly
Nested while loopsO(n^2)O(1)Alternative loop style
String.repeat (Java 11+)O(n^2)O(1)Concise code with modern Java
💡
Use nested loops where the outer loop controls rows and the inner loop controls stars per row.
⚠️
Beginners often forget to print a new line after each row, causing all stars to print on one line.