Right-Aligned Right-Angled Triangle Star Pattern in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spaces + stars

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:

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 <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

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.

Setup
2

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.

Row control
3

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.

Right align
4

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.

Stars
5

New line

System.out.println() ends the row. Every row has (rows - i) + i = rows characters before the newline.

Line break
=

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.

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 <= 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 > 0 after 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 - i and i between the two inner loops
  • Forgetting System.out.println() after each row
  • Using j < rows - i when you meant <= (off-by-one spacing)
  • Mixing tabs with spaces (alignment changes by editor)
  • Assuming input is valid without checking

Key Takeaways

1

Right alignment = print (rows - i) spaces, then i stars, per row.

2

Two inner loops are used because spaces and stars have different counts each row.

3

Star counts per row match Program 1; only the leading padding changes.

4

Time complexity is O(n²) for n rows (quadratic total output).

5

This pattern is the usual stepping stone to pyramids and diamonds that mix spaces and stars.

❓ Frequently Asked Questions

For row 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.
One loop prints only one type of character. Here we need both spaces and stars with different counts per row, so we use one loop for spaces and another for stars.
The left-aligned triangle prints only stars. The right-aligned triangle prints leading spaces first, then stars, so the stars align on the right edge.
Time complexity is O(n²) for n rows because each row prints about n characters (spaces + stars) across n rows.

Next: Inverted Right-Aligned Triangle

Continue to Program 4 to print the inverted right-aligned triangle star pattern in Java.

Program 4 →
Did you know?

Most centered patterns (pyramid, diamond) start with the same idea: print spaces first, then stars. This right-aligned triangle is a great stepping stone.

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.

14 people found this page helpful