Python Star Cross Pattern (Over Zeros)

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

What Is This Pattern?

An X pattern of stars and zeros fills a rectangular grid: print * on both diagonals and the center column; print 0 everywhere else.

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

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

Follows the number diamond in Program 44; next is the concentric number square in Program 46.

How to Solve It

One nested loop over the grid, plus a three-way star condition.

MethodIdeaBest for
Diagonals + midi==j / j==mid / anti-diagonalLearning, interviews, exams
Sized gridSame logic with rows, cols, midPractice / demos

Pseudocode

Pseudocode
mid = (cols + 1) / 2
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 rows / colsfor i in range(1, rows + 1): / for j in range(1, cols + 1):
Center columnmid = (cols + 1) // 2
Star testif i == j or j == mid or i == cols + 1 - j:
Print star / fillprint("*", end="") / print("0", end="")
End the rowprint()

Printing Numbers vs Starting a New Line

APIEffectUse for
print("*", end="") / print("0", end="")Stays on the same lineEach cell
print()Ends the current lineAfter the column loop finishes a row

Print each cell without a newline, then end the row once.

Live Preview

Change the row count (width becomes 2 × rows + 1) and the X updates instantly.

Whole numbers from 3 to 6. Columns = 2 × rows + 1 (odd width so the center column exists). Tap a chip or type a value — the preview redraws as you go.

Live result 4 × 9 · 36 cells
*000*000*
0*00*00*0
00*0*0*00
000***000

Worked Walkthrough — rows = 4, cols = 9

mid = 5. Trace why key cells print *.

CellWhy *?Row so far
(1,1)i == j (main diagonal)*
(1,5)j == mid*000*
(1,9)i == 10 - j*000*000*
(4,4)..(4,6)diagonal + mid meet000***000

Every cell is decided once — total prints = rows × cols.

Python Programs

Three complete programs: fixed 4×9, input() size, and diagonals-only contrast. Use View Output for sample results.

Example 1 — Fixed rows = 4, cols = 9

Hard-coded bounds with literals 5 and 10 - j.

Python
for i in range(1, 5):
    for j in range(1, 10):
        if i == j or j == 5 or i == 10 - j:
            print("*", end="")
        else:
            print("0", end="")
    print()

How It Works

1. Grid walk. Outer i picks the row; inner j picks the column.

2. Star test. i == j (main), j == 5 (center), or i == 10 - j (anti-diagonal).

3. Fill. Everything else prints 0; bare print() ends each row.

Example 2 — User Input (rows & cols)

Read size, compute mid, and use cols + 1 - j for the anti-diagonal.

Python
try:
    rows = int(input("Enter rows: "))
    cols = int(input("Enter cols (odd): "))
except ValueError:
    print("Please enter whole numbers.")
    raise SystemExit(1)

if rows < 1:
    print("Please enter a positive row count.")
    raise SystemExit(1)

if cols < 1 or cols % 2 == 0:
    print("Please enter a positive odd column count.")
    raise SystemExit(1)

mid = (cols + 1) // 2

for i in range(1, rows + 1):
    for j in range(1, cols + 1):
        if i == j or j == mid or i == cols + 1 - j:
            print("*", end="")
        else:
            print("0", end="")
    print()

How It Works

1. Validate size. Require positive rows and an odd cols so a true center column exists.

2. Same star rule. mid and cols + 1 - j replace the hard-coded 5 and 10 - j.

3. Safer input tip. Cap demos for a readable X:

Safer input tip
if rows < 1 or rows > 6 or cols % 2 == 0 or cols < rows:
    print("Use rows 1–6 and odd cols >= rows.")
    raise SystemExit(1)

Example 3 — Diagonals Only

Drop the center-column check — a pure X without the vertical line.

Python
for i in range(1, 5):
    for j in range(1, 10):
        if i == j or i == 10 - j:
            print("*", end="")
        else:
            print("0", end="")
    print()

How It Works

1. Two tests only. Main diagonal i == j and anti-diagonal i == 10 - j.

2. Compare. The middle column of zeros shows what j == mid added in Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

even cols

Even column count

There is no single center column. Prefer odd cols so mid is exact.

0-based

Loops from 0 with 1-based formulas

If indices are 0-based, adjust to i == j, j == mid, and i + j == cols - 1.

and

Use and instead of or

Almost no cells match all three tests at once. Border stars need or.

print() inside

print() inside the column loop

That puts every cell on its own line. Call bare print() only after the inner loop.

rows > cols

More rows than columns

The main diagonal exits the grid early. Keep rows <= cols for a clear X.

input()

Catch ValueError

Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.

Time and Space Complexity

ProgramTimeExtra space
Fixed / diagonals (Examples 1, 3)O(rows × cols)O(1)
User input (Example 2)O(rows × cols)O(1)

Every cell of the grid is visited once, so work is proportional to the product of rows and columns.

Key Takeaways

  • Rule: * on main diagonal, anti-diagonal, and center column; 0 elsewhere.
  • Size: prefer odd cols; mid = (cols + 1) // 2; anti-diagonal uses cols + 1 - j.
  • end="" vs print(): each cell stays on the line; bare print() advances after each row.
  • Complexity: O(rows × cols) time; O(1) extra space.

One line: visit every cell; print * on the X and center, 0 everywhere else.

Frequently Asked Questions

An X-style grid: * on both diagonals and the center column, with 0 filling the remaining cells (classic demo: 4×9).
With cols = 9, mid = (cols + 1) // 2 = 5. Checking j == mid draws the vertical center line.
Main diagonal: i == j. Anti-diagonal: i == cols + 1 - j (for cols = 9 that is i == 10 - j).
Program 44 prints a centered number diamond. Program 45 prints a rectangular * / 0 grid using diagonal and center conditions.
Use rows and cols variables, compute mid = (cols + 1) // 2, and use cols + 1 - j for the anti-diagonal — see Example 2.
O(rows × cols) because each cell is visited once.
Yes — drop the j == mid check for a pure X of diagonals only — see Example 3.
For 1-based indexing, row i meets column j on the anti-diagonal when i + j equals cols + 1.
Use try/except ValueError around int(input()) and require positive rows plus an odd cols — see Example 2.

Did you know?

Print * when i == j, j == mid, or i == cols + 1 - j; otherwise print 0. A rows × cols grid visits every cell once.

Next: Concentric Number Square

Print values that decrease toward the center and mirror back out (5..1..5).

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