Start with a fixed 4×9 grid, then parameterize rows/cols — optionally drop the center column for a pure X.
Method
Idea
Best for
Diagonals + center
Print * on i == j, j == mid, or anti-diagonal
Learning, interviews, exams
Diagonals only
Drop j == mid for a pure X
Seeing 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
Goal
Pattern
Visit every cell
for (i = 1; i <= rows; i++) / for (j = 1; j <= cols; j++)
Main diagonal
i == j
Center column
j == mid where mid = (cols + 1) / 2
Anti-diagonal
i == (cols + 1) - j
Print cell
printf("*") or printf("0")
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("*") / printf("0")
Stays on the same line
Each cell in the row
printf("\n")
Ends the current line
After the inner loop
Print all cells without a newline, then end the row once.
Try it
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 result4×9 · 36 cells
*000*000*
0*00*00*0
00*0*0*00
000***000
Trace
Worked Walkthrough — Row i = 2, cols = 9
Trace each column on row 2 and decide whether the cell is a star or a zero.
j
Condition hit?
Why
Prints
1
No
Interior
0
2
Yes
i == j
*
5
Yes
j == mid
*
8
Yes
i == 10 - j
*
9
No
Interior
0
Full row 2: 0*00*00*0. Total cells = rows × cols (here 36) — why time is O(rows × cols).
Code
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;
}
Output
*000*000*
0*00*00*0
00*0*0*00
000***000
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;
}
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;
}
Output
*0000000*
0*00000*0
00*000*00
000*0*000
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.