- 1Print Right Triangle Star Pattern in Java
- 2Print Inverted Right Triangle Star Pattern
- 3Print Mirrored Right Triangle Star Pattern in Java
- 4Print Inverted Mirrored Right Triangle Star Pattern
- 5Print Right Pascal's Triangle Star Pattern in Java
- 6Print K Shape Star Pattern in Java
- 7Print Mirrored Right Pascal's Triangle Pattern in Java
- 8Print Full Reverse K Star Pattern in Java
- 9Print Pyramid Star Pattern in Java
- 10Print Inverted Pyramid Star Pattern in Java
- 11Print Diamond Star Pattern in Java
- 12Print Hollow Diamond Star Pattern in Java
- 13Print Sandglass Star Pattern in Java
- 14Print Butterfly Star Pattern in Java
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:
- Use an outer loop to control the rows (from n down to 1).
- Use an inner loop to print stars — the number of stars equals the row number.
- 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();
}
⬅ Previous TopicPrint Right Triangle Star Pattern in Java
Next Topic ⮕Print Mirrored Right Triangle Star Pattern in Java
Next Topic ⮕Print Mirrored Right Triangle Star Pattern in Java
Comments
Loading comments...