Inverted V-Shaped Hollow Star Pattern in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

What You'll Learn

This program prints a hollow inverted V (one star on the first row, then two stars per row farther apart) using only i == j and i == k.

See Program 8 for the upright V (wide row first, vertex at the bottom). Each line has width 2 * rows - 1 (9 characters when rows = 5).

⭐ 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 = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (i == j) System.out.print("*");
                else System.out.print(" ");
            }
            for (int k = 2; k <= rows; k++) {
                if (i == k) System.out.print("*");
                else System.out.print(" ");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Setup

rows sets the height. i is the row index (1 to rows). j scans the left block; k scans the right block. Each cell is System.out.print("*") or System.out.print(" ").

Setup
2

Outer loop (rows)

for (int i = 1; i <= rows; i++) grows the gap between the two legs: row 1 is the apex; row rows has stars at both outer columns.

Row index
3

Left leg: j from rows down to 1

for (int j = rows; j >= 1; j--) with if (i == j) System.out.print("*"); else System.out.print(" "); draws the descending diagonal.

Left leg
4

Right leg: k from 2 to rows

for (int k = 2; k <= rows; k++) mirrors the left leg. Starting at k = 2 skips a duplicate apex star; for i > 1 you print two stars per row separated by spaces.

Right leg
5

Finish the line

System.out.println() ends the row. Each line has width rows + (rows - 1) = 2 * rows - 1 characters.

Line break
=

Hollow inverted V

Only diagonal positions get *; the interior stays spaces. O(n²) output for n = rows, O(1) extra space. Width 2n - 1 scrolls inside 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 = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (i == j) System.out.print("*");
                else System.out.print(" ");
            }
            for (int k = 2; k <= rows; k++) {
                if (i == k) System.out.print("*");
                else System.out.print(" ");
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Print the V-shaped hollow half by reversing the outer loop (Program 8)
  • Combine Program 7 + Program 8 to form a hollow diamond (Program 9)
  • Replace * with another character (like #)
  • Use a fixed-width font in the console for alignment
  • Validate rows > 0 before printing

Avoid

  • Starting the second loop from k = 1 (it duplicates the center)
  • Mixing tabs and spaces (alignment changes)
  • Forgetting the newline after each row
  • Printing stars on every position (it becomes filled)
  • Assuming user input is always valid

Key Takeaways

1

Each row is 2 * rows - 1 characters wide.

2

Stars appear only on the two diagonals: i == j and i == k.

3

The second loop starts from k = 2 to avoid duplicating the middle column.

4

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

5

This pattern is the upper half of the hollow diamond (Program 9).

❓ Frequently Asked Questions

The first inner loop already includes the center column. Starting the second loop from 2 avoids printing the middle position twice.
Print this pattern for the upper half (Program 7, inverted V), then print the lower half with Program 8’s outer-loop idea starting from rows - 1 to avoid duplicating the middle row (Program 9).
It’s O(n²) for n rows because each row prints Theta(n) characters and there are n rows.

Next: V-Shaped Hollow Pattern

Continue to Program 8 to print the upright V-shaped hollow pattern in Java.

Program 8 →
Did you know?

If you print Program 7 and then print Program 8 starting from rows - 1, you get the full hollow diamond (Program 9) without duplicating the middle row.

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