Java Star Cross Pattern (Over Zeros)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A star cross over zeros prints * on the main diagonal, anti-diagonal, and middle column; every other cell prints 0.

Remember
Rule: * if i==j or j==mid or i==cols+1-j; else 0
      mid = cols/2 + 1

*000*000*
0*00*00*0
00*0*0*00
000***000     ← rows = 4, cols = 9

Unlike Program 44 (number diamond), here every cell is either a cross * or a fill 0.

How to Solve It

Visit each cell in a rows × cols grid; print * on the three cross lines, else 0.

MethodIdeaBest for
Three-part ifMain diagonal + anti-diagonal + middle columnLearning, interviews, exams
X onlyDrop j == mid; keep both diagonalsWhen you want a plain X without the vertical bar

Pseudocode

Pseudocode
cols = 9
mid = cols / 2 + 1
for i from 1 to rows:
    for j from 1 to cols:
        if i == j or j == mid or i == cols + 1 - j:
            print "*"
        else:
            print "0"
    print newline

Cheat sheet

GoalPattern
Walk gridfor i = 1..rows; for j = 1..cols
Main diagonali == j
Anti-diagonali == cols + 1 - j
Middle columnj == mid where mid = cols / 2 + 1
End the rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach cell (* or 0)
System.out.printlnEnds the current lineAfter the column loop finishes

Print values without a newline, then end the row once.

Live Preview

Change the row count (columns stay at 9) and the cross updates instantly.

Whole numbers from 3 to 9 with fixed cols = 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 4 · cols = 9
*000*000*
0*00*00*0
00*0*0*00
000***000

Worked Walkthrough — rows = 4, row i = 4

Trace why the last row becomes 000***000 when cols = 9 and mid = 5.

jConditionPrinted
1..3None0
4i == j (main diagonal)*
5j == mid*
6i == cols + 1 - j*
7..9None0

Three adjacent stars meet in the center — that is the classic 000***000 row.

Java Programs

Three complete programs: fixed size, Scanner rows input, and an X-only cross. Use View Output to reveal sample results.

Example 1 — Fixed rows = 4, cols = 9

Hard-coded size — three-part if builds the cross on a zero grid.

Java
public class StarCrossPattern {
    public static void main(String[] args) {
        int rows = 4;
        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();
        }
    }
}

How It Works

1. Grid loops. Outer i is the row; inner j is the column.

2. Cross test. Main diagonal, middle column, or anti-diagonal → print *.

3. Fill. Everything else prints 0.

When i = 1: stars at columns 1, 5, and 9. When i = 4: stars at columns 4, 5, and 6.

Example 2 — Rows Input

Read rows at runtime; columns stay fixed at 9.

Java
import java.util.Scanner;

public class StarCrossPatternInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
        if (rows < 1) return;

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

How It Works

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Same core. Only the source of rows changes; cols stays 9.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — X Only

Drop the middle-column check for a plain X without the vertical bar.

Java
public class StarCrossXOnly {
    public static void main(String[] args) {
        int rows = 4;
        int cols = 9;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                if (i == j || i == cols + 1 - j)
                    System.out.print("*");
                else
                    System.out.print("0");
            }
            System.out.println();
        }
    }
}

How It Works

1. Same grid. Still rows × cols nested loops.

2. Two lines only. Main and anti-diagonals — no j == mid.

3. Compare tip. Put Examples 1 and 3 side by side to see what the middle column adds.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of cells

If println is inside the column loop, each cell lands on its own line. Use print for cells; println only after the row.

Wrong anti

Broken X

Using i + j == cols instead of i == cols + 1 - j shifts the anti-diagonal off by one.

Even cols

No true center

Odd cols gives one middle column. Even cols makes mid lean left of center.

No println

One long line

Forgetting println() after the row glues every cell onto one endless line.

rows = 1

Single cross row

Output is one line with stars on the main diagonal, middle, and anti-diagonal positions that apply.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Star cross (Examples 1–3)O(rows × cols)O(1)

Both loops visit every cell once. Only loop counters are stored.

Key Takeaways

  • Rule: print * on i==j, j==mid, or i==cols+1-j; else 0.
  • Odd cols: use an odd column count so mid is a true center.
  • Break the row: call println only after the column loop.
  • Complexity: O(rows × cols) time; O(1) extra space.

One line: for each cell, print * if it is on a diagonal or the middle column, else 0, then println() after each row.

Frequently Asked Questions

It draws an X (both diagonals) and a vertical middle line using *. All other positions are filled with 0.
Because the pattern uses 9 columns, and mid = cols/2 + 1 = 5. For an odd column count, there is a single center column.
For cols=9, the anti-diagonal satisfies i == cols+1-j (equivalently i == 10-j).
On row i=4, the diagonals hit columns 4 and 6, and the middle column is 5 — three adjacent * characters in the center.
Yes. Delete the condition j == mid. Keep only i == j and i == cols+1-j — see Example 3.
It works best with an odd number of columns so there is a single middle column. Even columns change the center behavior.
O(rows*cols) because the nested loops visit each cell once.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

A * prints on the main diagonal (i==j), anti-diagonal (i==cols+1-j), and middle column (j==mid). Every other cell prints 0.

Next: Concentric Number Square

Move on to the concentric number square in the Java number-pattern series.

Program 46 tutorial →

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