Inverted Right-Aligned Triangle Star Pattern in Java

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

What You'll Learn

This program prints an inverted right-aligned triangle. The first row prints rows stars, and each next row prints one less star, while leading spaces increase to keep the right edge aligned.

For row i (from rows down to 1), print rows - i spaces and then print 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 = rows; i >= 1; 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 counts rows from rows down to 1; j prints spaces; k prints stars.

Setup
2

Outer loop (rows, reverse)

for (int i = rows; i >= 1; i--) starts with the widest row (i == rows) and ends at a single star.

Row control
3

Padding: System.out.print(" ")

for (int j = 1; j <= rows - i; j++) prints rows - i spaces. On the first row that count is zero; it grows as i shrinks.

Right align
4

Stars: System.out.print("*")

for (int k = 1; k <= i; k++) prints i stars after the padding.

Stars
5

New line

System.out.println() ends each row. Characters before the newline: (rows - i) + i = rows every time.

Line break
=

Inverted right-aligned triangle

Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Wide rows scroll horizontally in 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 = rows; i >= 1; 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

  • Compare with Program 3 to see how reversing the outer loop changes the shape
  • Print a centered inverted pyramid by switching to odd stars + more spaces
  • Print a hollow version (stars only on the borders)
  • Use a different character (like #)
  • Validate that rows > 0

Avoid

  • Forgetting that spaces increase as stars decrease (alignment breaks)
  • Off-by-one errors in the space loop (rows - i)
  • Mixing tabs and spaces (output alignment changes)
  • Skipping newline after each row
  • Assuming input is valid without checking

Key Takeaways

1

Inverted right-aligned = print (rows - i) spaces, then i stars, with i decreasing.

2

The outer loop runs backward (rows to 1) to shrink the star count each row.

3

Spaces increase by 1 each row, keeping the right edge aligned.

4

Time complexity is O(n²) for n rows.

5

This pattern is a stepping stone to centered inverted pyramids and diamond shapes.

❓ Frequently Asked Questions

Spaces are printed first to push the stars to the right side. As the star count decreases, the space count increases so the right edge remains aligned.
Use an outer loop that increases from 1 to rows (Program 3). The space loop stays rows - i, but stars become i as i increases.
It’s O(n²) for n rows because each row prints about n characters (spaces + stars) and there are n rows.

Next: Pyramid Pattern

Continue to Program 5 to print a centered pyramid star pattern in Java.

Program 5 →
Did you know?

Right-aligned patterns are excellent practice before moving to centered pyramids, because both require printing spaces and stars with the correct counts.

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.

13 people found this page helpful