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.

Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗