- 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 Full Reverse K Star Pattern in Java
Topic
In this article, we will learn how to print a Full Reverse K Star Pattern in Java. This pattern consists of two joined triangle shapes—one decreasing and one increasing—with a center line of a single star. It visually resembles a mirror image of the letter 'K' and is a great exercise in loop control and symmetry logic.
To build this, we use:
for
loops for rows and columns- Nested loops to print spaces and stars
- Simple conditional logic to decide how many stars and spaces to print per row
Examples
Input: n = 5
*****
****
***
**
*
**
***
****
*****
Input: n = 3
***
**
*
**
***
Input: n = 1
*
Interviewer Expectations
The interviewer wants to evaluate your understanding of:
- Looping constructs like
for
loops - Printing patterns using nested loops
- Managing spaces and alignment
- Clean code and logical problem solving
Approach
The pattern is divided into two halves:
- Top half (including middle line): Decreasing triangle with increasing spaces
- Bottom half: Increasing triangle with decreasing spaces
For the top half (rows 1 to n):
- Print
i - 1
spaces - Print
n - i + 1
stars
For the bottom half (rows 1 to n - 1):
- Print
n - i - 1
spaces - Print
i + 1
stars
Dry Run (n = 3)
Top Half:
Row 1: spaces = 0, stars = 3 → ***
Row 2: spaces = 1, stars = 2 → **
Row 3: spaces = 2, stars = 1 → *
Bottom Half:
Row 1: spaces = 1, stars = 2 → **
Row 2: spaces = 0, stars = 3 → ***
Java Program
public class ReverseKFullPattern {
public static void main(String[] args) {
int n = 5;
// Top half
for (int i = 1; i <= n; i++) {
for (int space = 1; space < i; space++) {
System.out.print(" ");
}
for (int star = 1; star <= n - i + 1; star++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half
for (int i = 1; i < n; i++) {
for (int space = 1; space < n - i; space++) {
System.out.print(" ");
}
for (int star = 1; star <= i + 1; star++) {
System.out.print("*");
}
System.out.println();
}
}
}
*****
****
***
**
*
**
***
****
*****
Possible Followup Questions with Answers
1. How do you make the pattern right-aligned?
By printing the correct number of spaces before stars using nested loops.
2. How to accept user input for number of rows?
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
3. What is the total number of rows in the pattern?
The pattern has 2 * n - 1
rows (top half + bottom half excluding the center repeated).
4. Can this be done with a single loop?
Not efficiently. The pattern requires careful handling of two different shapes, so separate loops improve readability and logic clarity.
Next Topic ⮕Print Pyramid Star Pattern in Java
Comments
Loading comments...