Print Inverted Right Triangle Star Pattern

Topic

In this program, we will print an inverted right triangle pattern using stars (*) in Java. This type of pattern is a common question in beginner-level interviews to test understanding of loops and pattern logic.

The triangle is "inverted" because the longest row appears first, and each subsequent row has one less star.

Examples

Input: n = 5

*****
****
***
**
*

Input: n = 3

***
**
*

Edge Case - Input: n = 1

*

Interviewer Expectations

Interviewers ask this to see how comfortable you are with:

  • Nested loops (loop inside another loop)
  • Controlling the number of characters per row
  • Basic understanding of conditional logic and iterative constructs
  • Writing clean and structured code

Approach

To print this pattern:

  1. Use an outer loop to control the rows (from n down to 1).
  2. Use an inner loop to print stars — the number of stars equals the row number.
  3. Move to the next line after printing all stars for the current row.

Dry Run for n = 4

Row 1: ****
Row 2: ***
Row 3: **
Row 4: *

Java Program

public class InvertedTrianglePattern {
    public static void main(String[] args) {
        int n = 5;
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
*****
****
***
**
*

Possible Followup Questions with Answers

1. How do you print a right-angled triangle instead of an inverted one?

Just reverse the outer loop to go from 1 to n.

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

2. How would you print numbers instead of stars?

Change System.out.print("*") to System.out.print(j) or System.out.print(i) depending on the requirement.

3. How can you right-align the inverted triangle?

Print spaces before the stars in each row:

for (int i = n; i >= 1; i--) {
    for (int s = 0; s < n - i; s++) {
        System.out.print(" ");
    }
    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...