Star Cross Pattern with 0s in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
*000*000*
0*00*00*0
00*0*0*00
000***000
1

Complete Java Program

Use three checks inside the inner loop: diagonal, center column, and anti-diagonal.

Java
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

1

Choose the grid size

Here we print rows=4 lines, each with cols=9 characters.

Setup
2

Outer loop prints each row

i goes from 1 to rows.

Rows
3

Inner loop prints each column

j goes from 1 to cols. Each (i,j) pair decides whether to print * or 0.

Columns
4

Three conditions draw the cross

Print * when i==j (main diagonal), or j==mid (middle column), or i==cols+1-j (anti-diagonal).

Logic
=

Star cross over zeros

The two diagonals form an X. The middle column adds a vertical line through the center.

2

Variation — User Input Version

Let the user choose the number of rows (columns stay 9 to preserve the same pattern width):

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();

        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 cols as an odd number (like 7, 9, 11) so the middle column is clear
  • Swap symbols (print 1 instead of *)
  • Print a full X without the middle column by removing j == mid
  • Change the background fill from 0 to . for readability
  • Convert it to a square by setting rows = cols and using the same diagonal logic

Avoid

  • Using even cols if 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.in with Scanner if you need console input later in the same JVM run

Key Takeaways

1

Use i==j to draw the main diagonal.

2

Use i==cols+1-j to draw the anti-diagonal.

3

Use j==mid to draw the middle column.

4

Everything else prints 0, creating contrast for the star cross.

❓ Frequently Asked Questions

On row i=4, the diagonals hit columns 4 and 6, and the middle column is 5. Those three adjacent positions print *, producing *** in the center.
Yes. Delete the condition j == mid. Keep only i == j and i == cols + 1 - j.
It works best with an odd number of columns so there is a single middle column. For even columns, you’ll have two middle columns and the pattern changes.
O(rows*cols) because the nested loops visit each cell once.

Explore More Java Number Patterns!

Symbol patterns like X, crosses, and grids are excellent practice for conditionals inside nested loops.

All Number Patterns →
Did you know?

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.

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.

12 people found this page helpful