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.
Approach
How to Solve It
Visit each cell in a rows × cols grid; print * on the three cross lines, else 0.
Method
Idea
Best for
Three-part if
Main diagonal + anti-diagonal + middle column
Learning, interviews, exams
X only
Drop j == mid; keep both diagonals
When 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
Goal
Pattern
Walk grid
for i = 1..rows; for j = 1..cols
Main diagonal
i == j
Anti-diagonal
i == cols + 1 - j
Middle column
j == mid where mid = cols / 2 + 1
End the row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each cell (* or 0)
System.out.println
Ends the current line
After the column loop finishes
Print values without a newline, then end the row once.
Try it
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 resultrows = 4 · cols = 9
*000*000*
0*00*00*0
00*0*0*00
000***000
Trace
Worked Walkthrough — rows = 4, row i = 4
Trace why the last row becomes 000***000 when cols = 9 and mid = 5.
j
Condition
Printed
1..3
None
0
4
i == j (main diagonal)
*
5
j == mid
*
6
i == cols + 1 - j
*
7..9
None
0
Three adjacent stars meet in the center — that is the classic 000***000 row.
Code
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();
}
}
}
Output
*000*000*
0*00*00*0
00*0*0*00
000***000
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();
}
}
Output (when user enters 4)
Enter the number of rows: 4
*000*000*
0*00*00*0
00*0*0*00
000***000
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();
}
}
}
Output
*0000000*
0*00000*0
00*000*00
000*0*000
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Star cross (Examples 1–3)
O(rows × cols)
O(1)
Both loops visit every cell once. Only loop counters are stored.
Remember
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.