Right-Angled Triangle Star Pattern in Java

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:
*
**
***
****
*****Complete Java Program
Fixed rows = 5 version:
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
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.
Outer loop (rows)
for (int i = 1; i <= rows; i++) walks each row from 1 through rows.
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.
New line
System.out.println() ends the row after the inner loop so the next i starts on a fresh line.
Right-angled triangle
Total stars: 1+2+…+n = n(n+1)/2 — O(n²) output for n = rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.
Variation — User Input Version
Accept rows with Scanner:
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.inif you need to read input again later in the same app
Key Takeaways
The outer loop controls rows, and the inner loop prints stars for the current row.
Row i prints exactly i stars, producing the right-angled triangle.
Total stars are n(n + 1)/2, so time complexity is O(n²).
This pattern is a foundation for pyramids, diamonds, hollow shapes, and mirrored patterns.
Make the program dynamic by reading rows using Scanner.
❓ Frequently Asked Questions
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.for (int i = rows; i >= 1; i--). This starts with rows stars and decreases by 1 each row (Program 2).Next: Inverted Right-Angled Triangle
Continue to Program 2 to print the inverted right-angled triangle pattern in Java.
Once you’re comfortable with this pattern, try printing it inverted (Program 2) and then try centered pyramids (Programs 5 and 6).
16 people found this page helpful
