Star Cross Pattern with 0s in Java

What You’ll Learn
How to print a symbol pattern in Java that draws:
- the main diagonal (
i == j) - the anti-diagonal (
i == cols + 1 - j) - the middle column (
j == mid)
All other positions are filled with 0, producing a clean “cross/X” look.
⭐ Pattern Output
For rows = 4 and cols = 9, the pattern looks like this:
*000*000*
0*00*00*0
00*0*0*00
000***000Complete Java Program
Use three checks inside the inner loop: diagonal, center column, and anti-diagonal.
public class Main {
public static void main(String[] args) {
int rows = 4;
int cols = 9;
int mid = cols / 2 + 1; // 5 when cols=9
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == j || j == mid || i == cols + 1 - j) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}🧠 How It Works
Choose the grid size
Here we print rows=4 lines, each with cols=9 characters.
Outer loop prints each row
i goes from 1 to rows.
Inner loop prints each column
j goes from 1 to cols. Each (i,j) pair decides whether to print * or 0.
Three conditions draw the cross
Print * when i==j (main diagonal), or j==mid (middle column), or i==cols+1-j (anti-diagonal).
Star cross over zeros
The two diagonals form an X. The middle column adds a vertical line through the center.
Variation — User Input Version
Let the user choose the number of rows (columns stay 9 to preserve the same pattern width):
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();
int cols = 9;
int mid = cols / 2 + 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
if (i == j || j == mid || i == cols + 1 - j) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Use
colsas an odd number (like 7, 9, 11) so the middle column is clear - Swap symbols (print
1instead of*) - Print a full X without the middle column by removing
j == mid - Change the background fill from
0to.for readability - Convert it to a square by setting
rows = colsand using the same diagonal logic
Avoid
- Using even
colsif you want a single center column - Mixing row/column bounds (make sure anti-diagonal uses
cols) - Forgetting
System.out.println()after each row - Closing
System.inwithScannerif you need console input later in the same JVM run
Key Takeaways
Use i==j to draw the main diagonal.
Use i==cols+1-j to draw the anti-diagonal.
Use j==mid to draw the middle column.
Everything else prints 0, creating contrast for the star cross.
❓ Frequently Asked Questions
i=4, the diagonals hit columns 4 and 6, and the middle column is 5. Those three adjacent positions print *, producing *** in the center.j == mid. Keep only i == j and i == cols + 1 - j.Explore More Java Number Patterns!
Symbol patterns like X, crosses, and grids are excellent practice for conditionals inside nested loops.
Many “drawing” patterns are just grid problems. If you can describe which cells belong to the shape (diagonals, borders, center lines), you can print almost anything with nested loops.
12 people found this page helpful
