V-Shaped Hollow Star Pattern in Java

What You'll Learn
This program prints a hollow V (two outer stars on the first row, then closer together each row until one star on the last row). See Program 7 for the inverted V. It is the lower half building block for the hollow diamond (Program 9).
Each line has width 2 * rows - 1 (9 characters when rows = 5).
⭐ Pattern Output
When you run the program with rows = 5:
* *
* *
* *
* *
* Complete Java Program
Fixed rows = 5 version:
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; 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
Setup
rows, i, j, k match the hollow-V pattern: two inner loops per row, each cell is System.out.print("*") or System.out.print(" ") from if (i == j) / if (i == k).
Outer loop (reverse rows)
for (int i = rows; i >= 1; i--) prints the widest pair of legs first. The last row (i == 1) only gets a star from the j loop—the k loop starts at 2, so the bottom is a single apex star with leading spaces.
Left leg: j from rows down to 1
for (int j = rows; j >= 1; j--) with if (i == j) places the left diagonal. As i counts down, the star in this block moves leftward row by row.
Right leg: k from 2 to rows
for (int k = 2; k <= rows; k++) mirrors the left leg. When i > 1, you print a second *; when i == 1, this loop never emits a star.
Finish the line
System.out.println() ends each row. Line width is still 2 * rows - 1, same geometry as the upward hollow V, only the row order is reversed.
Hollow V
Vertex at the bottom, opening upward. O(n²) output for n = rows, O(1) extra space. Wide rows use horizontal scroll in the green preview on small screens. Stack with Program 7 to picture a full hollow diamond.
Variation — User Input Version
Accept rows with Scanner:
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 = rows; i >= 1; 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 inverted hollow V (Program 7) to see the mirror
- Build a hollow diamond by printing Program 7 then printing this starting from
rows - 1(Program 9) - Replace
*with another symbol - Use fixed-width font for clean alignment
- Validate
rows > 0before printing
Avoid
- Starting the second loop from
k = 1(center duplicates) - Forgetting the newline after each row
- Mixing tabs and spaces
- Printing stars everywhere (becomes filled)
- Assuming user input is always valid
Key Takeaways
Each row is 2 * rows - 1 characters wide.
Stars appear only on the two diagonals in each row (hollow pattern).
Start the second loop at k = 2 to avoid duplicating the center column.
Time complexity is O(n²) for n rows.
This is the lower-half building block for the hollow diamond (Program 9).
❓ Frequently Asked Questions
2 avoids printing the middle position twice.rows - 1 to avoid duplicating the middle row (Program 9).Next: Hollow Diamond Pattern
Continue to Program 9 to combine the two halves into a hollow diamond.
If you start this descending row loop from rows - 1, you get the lower half of the hollow diamond without printing the middle row twice.
12 people found this page helpful
