Inverted V-Shaped Hollow Star Pattern in Java

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:
*
* *
* *
* *
* *Complete Java Program
Fixed rows = 5 version:
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
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(" ").
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.
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.
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.
Finish the line
System.out.println() ends the row. Each line has width rows + (rows - 1) = 2 * rows - 1 characters.
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.
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 = 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 > 0before 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
Each row is 2 * rows - 1 characters wide.
Stars appear only on the two diagonals: i == j and i == k.
The second loop starts from k = 2 to avoid duplicating the middle column.
Time complexity is O(n²) for n rows.
This pattern is the upper half of 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: V-Shaped Hollow Pattern
Continue to Program 8 to print the upright V-shaped hollow pattern in Java.
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.
13 people found this page helpful
