C Star Cross Pattern (Over Zeros)

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

What Is This Pattern?

An X pattern with stars and zeros prints * on both diagonals and the center column of a grid, and fills every other cell with 0.

Remember
Rule: for each cell (i, j)
        if i == j or j == mid or i == cols + 1 - j: print *
        else: print 0

*000*000*
0*00*00*0
00*0*0*00
000***000     ← 4 × 9

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

How to Solve It

Start with a fixed 4×9 grid, then parameterize rows/cols — optionally drop the center column for a pure X.

MethodIdeaBest for
Diagonals + centerPrint * on i == j, j == mid, or anti-diagonalLearning, interviews, exams
Diagonals onlyDrop j == mid for a pure XSeeing what the center line adds

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
Visit every cellfor (i = 1; i <= rows; i++) / for (j = 1; j <= cols; j++)
Main diagonali == j
Center columnj == mid where mid = (cols + 1) / 2
Anti-diagonali == (cols + 1) - j
Print cellprintf("*") or printf("0")

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("*") / printf("0")Stays on the same lineEach cell in the row
printf("\n")Ends the current lineAfter the inner loop

Print all cells without a newline, then end the row once.

Live Preview

Change rows and columns and the X pattern updates instantly — use an odd column count so the center line sits in the middle.

Rows 2–9, odd cols 3–15. Tap a chip or type values — 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 — Row i = 2, cols = 9

Trace each column on row 2 and decide whether the cell is a star or a zero.

jCondition hit?WhyPrints
1NoInterior0
2Yesi == j*
5Yesj == mid*
8Yesi == 10 - j*
9NoInterior0

Full row 2: 0*00*00*0. Total cells = rows × cols (here 36) — why time is O(rows × cols).

C Programs

Three complete programs: fixed 4×9, parameterized size, and diagonals-only. Use View Output to reveal sample results.

Example 1 — Fixed rows = 4, cols = 9

Hard-coded grid — * on diagonals and column 5, 0 elsewhere.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if (i == j || j == 5 || i == 10 - j)
                printf("*");
            else
                printf("0");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Visit every cell. Nested loops cover all 36 positions in the 4×9 grid.

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

3. End the line. printf("\n") runs only after the inner loop finishes the row.

When i = 1, j = 1 you get *; when i = 2, j = 3 you get 0.

Example 2 — Variable Rows and Columns

Compute mid and use (cols + 1) - j so the pattern scales cleanly.

C
#include <stdio.h>

int main(void)
{
    int rows = 4, cols = 9;
    int mid = (cols + 1) / 2;
    int i, j;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= cols; j++)
        {
            if (i == j || j == mid || i == (cols + 1) - j)
                printf("*");
            else
                printf("0");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Derive mid. mid = (cols + 1) / 2 replaces the literal 5.

2. Same core. (cols + 1) - j replaces 10 - j so the anti-diagonal tracks width.

3. Resize tip. Prefer an odd cols so the center column sits exactly in the middle.

Example 3 — Diagonals Only (No Center Column)

Remove j == mid from the condition — only the two diagonals print stars.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 9; j++)
        {
            if (i == j || i == 10 - j)
                printf("*");
            else
                printf("0");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Same loops. Only the condition changes — drop the center-column check.

2. Pure X. Row 4 becomes 000*0*000 instead of 000***000.

3. Compare with Example 1. One condition is the difference between an X and an X-plus-center line.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong anti

Broken mirror diagonal

Using i == cols - j instead of cols + 1 - j shifts the anti-diagonal. Keep the + 1 for 1-based indexes.

even cols

Off-center mid

With even cols, (cols + 1) / 2 is not a true center. Prefer an odd width for this lesson.

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each cell lands on its own line. Call the newline only after the row finishes.

hard-coded 5

Broken resize

If you change cols but leave j == 5 and 10 - j, the X breaks. Use mid and cols + 1 - j.

rows > cols

Short diagonals

When rows > cols, i == j never reaches the last rows. That is expected — diagonals stop at the smaller bound.

spaces

Missing fill

Printing nothing for non-star cells collapses the grid. Always print 0 (or another fill) in the else branch.

Time and Space Complexity

ProgramTimeExtra space
Fixed / parameterized (Examples 1–2)O(rows × cols)O(1)
Diagonals only (Example 3)O(rows × cols)O(1)

Every cell is visited once. For 4×9 that is 36 prints.

Key Takeaways

  • Rule: print * when i == j, j == mid, or i == cols + 1 - j; else 0.
  • Parameterize: use mid and cols + 1 - j so resizing does not break the X.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(rows × cols) time; O(1) extra space.

One line: for each cell, print * on a diagonal or center column, else 0, then printf("\n") after each row.

Frequently Asked Questions

An X-style pattern using * on the two diagonals and the center column, filling remaining positions with 0 on a 4x9 grid.
The width is 9 columns (j = 1..9). The middle column is 5, so checking j == 5 prints a vertical center line.
The left-to-right diagonal uses i == j. The right-to-left diagonal uses i == cols + 1 - j (10 - j when cols is 9).
Program 44 prints a centered number diamond with ascending digits. Program 45 prints a fixed grid with * and 0 using diagonal and center conditions.
Use rows and cols variables, compute mid = (cols + 1) / 2, and replace 10 - j with (cols + 1) - j — see Example 2.
O(rows × cols) because each cell is visited once in the nested loops.
Yes — drop the j == mid check to get 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.

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 — total prints = rows × cols.

Next: Concentric Number Square

Continue with values that decrease toward the center and mirror back out.

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