Right-Angled Triangle Star Pattern in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

What You'll Learn

This program prints a right-angled triangle where each next row contains one more star than the previous row.

Row i prints exactly i stars.

⭐ Pattern Output

When you run the program with rows = 5:

Output
*
**
***
****
*****
1

Complete Java Program

Fixed rows = 5 version:

Java
public class Main {
    public static void main(String[] args) {
        int rows = 5;

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

🧠 How It Works

1

Entry point and variables

public static void main(String[] args) is the program entry. int rows sets how many lines to print; i and j index the outer row loop and inner star loop.

Setup
2

Outer loop (rows)

for (int i = 1; i <= rows; i++) walks each row from 1 through rows.

Row control
3

Inner loop: System.out.print("*")

for (int j = 1; j <= i; j++) prints exactly i asterisks on the current line without advancing to the next line yet.

Stars
4

New line

System.out.println() ends the row after the inner loop so the next i starts on a fresh line.

Line break
=

Right-angled triangle

Total stars: 1+2+…+n = n(n+1)/2O(n²) output for n = rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.

2

Variation — User Input Version

Accept rows with Scanner:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Accept row count from the user with Scanner
  • Print an inverted triangle by reversing the outer loop (Program 2)
  • Replace * with numbers for a number triangle
  • Add spaces between stars for visual variety
  • Create a hollow triangle (print only border stars)

Avoid

  • Hard-coding the row count in real programs
  • Forgetting System.out.println() between rows
  • Using string concatenation in a tight loop for large patterns (prefer StringBuilder)
  • Not handling invalid user input (non-integer values)
  • Closing System.in if you need to read input again later in the same app

Key Takeaways

1

The outer loop controls rows, and the inner loop prints stars for the current row.

2

Row i prints exactly i stars, producing the right-angled triangle.

3

Total stars are n(n + 1)/2, so time complexity is O(n²).

4

This pattern is a foundation for pyramids, diamonds, hollow shapes, and mirrored patterns.

5

Make the program dynamic by reading rows using Scanner.

❓ Frequently Asked Questions

The outer loop runs from i = 1 to rows. For each row i, the inner loop runs from j = 1 to i, printing one star per iteration. So row 1 gets 1 star, row 2 gets 2 stars, and so on—forming the right-angled triangle.
Nested loops give two-dimensional control: the outer loop handles which row we’re on, and the inner loop handles how many stars to print on that row. This two-level control is required for most 2D patterns.
Yes. Reverse the outer loop: for (int i = rows; i >= 1; i--). This starts with rows stars and decreases by 1 each row (Program 2).
The time complexity is O(n²) where n is the number of rows. The inner loop prints 1+2+3+…+n = n(n+1)/2 total stars, which simplifies to O(n²).

Next: Inverted Right-Angled Triangle

Continue to Program 2 to print the inverted right-angled triangle pattern in Java.

Program 2 →
Did you know?

Once you’re comfortable with this pattern, try printing it inverted (Program 2) and then try centered pyramids (Programs 5 and 6).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful