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
In C# walk every cell with nested loops. Use mid = (cols + 1) / 2 and a three-part condition to choose * or 0.
Approach
How to Solve It
One nested loop over the grid, plus a three-way star condition.
Method
Idea
Best for
Diagonals + mid
i==j / j==mid / anti-diagonal
Learning, interviews, exams
Sized grid
Same logic with rows, cols, mid
Practice / 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
Goal
Pattern
Walk rows / cols
for (i = 1; i <= rows; i++) / for (j = 1; j <= cols; j++)
Center column
mid = (cols + 1) / 2
Star test
if (i == j || j == mid || i == cols + 1 - j)
Print star / fill
Console.Write("*"); / Console.Write("0");
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each * or 0
Console.WriteLine
Ends the current line
After the column loop finishes a row
Try it
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 result4 × 9 · 36 cells
*000*000*
0*00*00*0
00*0*0*00
000***000
Trace
Worked Walkthrough — rows = 4, cols = 9
mid = 5. Trace why key cells print *.
Cell
Why *?
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 meet
000***000
Every cell is decided once — total prints = rows × cols.
Code
C# Programs
Three complete programs: fixed 4×9, user-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.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 9; j++)
{
if (i == j || j == 5 || i == 10 - j)
Console.Write("*");
else
Console.Write("0");
}
Console.WriteLine();
}
}
}
}
Output
*000*000*
0*00*00*0
00*0*0*00
000***000
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; WriteLine ends each row.
Example 2 — User Input (rows & cols)
Read size, compute mid, and use cols + 1 - j for the anti-diagonal.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, cols, mid, i, j;
Console.Write("Enter rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
Console.Write("Enter cols (odd): ");
if (!int.TryParse(Console.ReadLine(), out cols) || cols < 1 || cols % 2 == 0)
{
Console.WriteLine("Please enter a positive odd column count.");
return;
}
mid = (cols + 1) / 2;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == j || j == mid || i == cols + 1 - j)
Console.Write("*");
else
Console.Write("0");
}
Console.WriteLine();
}
}
}
}
Output (when user enters 4 and 9)
Enter rows: 4
Enter cols (odd): 9
*000*000*
0*00*00*0
00*0*0*00
000***000
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.
Example 3 — Diagonals Only
Drop the center-column check — a pure X without the vertical line.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 9; j++)
{
if (i == j || i == 10 - j)
Console.Write("*");
else
Console.Write("0");
}
Console.WriteLine();
}
}
}
}
Output
*0000000*
0*00000*0
00*000*00
000*0*000
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 && instead of ||
Almost no cells match all three tests at once. Border stars need ||.
WriteLine
WriteLine inside the column loop
That puts every cell on its own line. Call WriteLine 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.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
Key Takeaways
Rule:* on main diagonal, anti-diagonal, and center column; 0 elsewhere.