Right-Aligned Right-Angled Triangle Star Pattern in Java

What You'll Learn
How to print a right-aligned right-angled triangle in Java: you print (rows - i) spaces first and then print i stars, so the stars line up on the right edge.
With rows = 5, the first row has four spaces and one star; the last row has no spaces and five 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 <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}🧠 How It Works
Setup
rows is the height. i indexes the row; j runs the space loop; k runs the star loop. Nested for loops live inside main.
Outer loop (rows)
for (int i = 1; i <= rows; i++) walks from one star on line 1 up to rows stars on the last line.
Padding: System.out.print(" ")
for (int j = 1; j <= rows - i; j++) prints rows - i spaces so the star block shifts right while the right edge stays straight.
Stars: System.out.print("*")
for (int k = 1; k <= i; k++) prints exactly i asterisks after the padding—same star count per row as Program 1, different horizontal offset.
New line
System.out.println() ends the row. Every row has (rows - i) + i = rows characters before the newline.
Right-aligned triangle
Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.
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 <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after reading input - Remove the space loop to get the left-aligned triangle (Program 1)
- Try the inverted right-aligned version (Program 4) by reversing the outer loop
- Print digits instead of
*for a right-aligned number triangle - Try a hollow right-aligned triangle (border only)
Avoid
- Swapping
rows - iandibetween the two inner loops - Forgetting
System.out.println()after each row - Using
j < rows - iwhen you meant<=(off-by-one spacing) - Mixing tabs with spaces (alignment changes by editor)
- Assuming input is valid without checking
Key Takeaways
Right alignment = print (rows - i) spaces, then i stars, per row.
Two inner loops are used because spaces and stars have different counts each row.
Star counts per row match Program 1; only the leading padding changes.
Time complexity is O(n²) for n rows (quadratic total output).
This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.
❓ Frequently Asked Questions
i, print (rows - i) spaces, then i stars. The spaces push the stars to the right so the hypotenuse lines up on the right side.Next: Inverted Right-Aligned Triangle
Continue to Program 4 to print the inverted right-aligned triangle star pattern in Java.
Most centered patterns (pyramid, diamond) start with the same idea: print spaces first, then stars. This right-aligned triangle is a great stepping stone.
14 people found this page helpful
