Shape Rule
* on X + center
* when on a diagonal or center column; 0 fills every other cell.

Program 45 prints an X-style grid: * on both diagonals and the center column, 0 everywhere else on a 4 × 9 rectangle — a natural step after Program 44’s centered number diamond. This tutorial covers nested loops with multi-condition checks, a live preview, worked C# examples, edge cases, and complexity.
* on X + center
* when on a diagonal or center column; 0 fills every other cell.
i = 1..rows
for (i = 1; i <= rows; i++) walks each row of the grid.
j = 1..cols
for (j = 1; j <= cols; j++) walks each column within the current row.
Diagonals + mid
i == j || j == mid || i == cols + 1 - j prints *; else print 0.
4×9 default
Adjust rows and odd column width, then draw the star-and-zero X in the browser.
Complexity
Each cell visited once — rows × cols prints; extra memory stays O(1).
A star-and-zero X pattern prints * on both diagonals and the center column, filling every other cell with 0. With rows = 4 and cols = 9, the output forms a compact cross on a rectangular grid.
In C# the outer loop runs i = 1..rows, the inner loop runs j = 1..cols, and a three-part condition picks "*" or "0" per cell.
It teaches diagonal math and multi-condition checks on a 2D grid — a key step after Program 44’s centered diamond.
i == j left-to-right.
i == cols + 1 - j.
Program 44 prints ascending digits in a diamond; Program 45 prints * and 0 on a fixed grid.
Follow Program 44; continue to Program 46 next.
In short: nested loops over i, j, three-way check prints *, else 0, then WriteLine().
Given a 4 × 9 grid, print * on both diagonals and the center column; fill remaining cells with 0.
// rows = 4, cols = 9
//*000*000*
//0*00*00*0
//00*0*0*00
//000***000 | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (e.g. 4). |
cols | int | Number of columns (e.g. 9 — odd width gives a clear center). |
mid | int | Center column: (cols + 1) / 2 (e.g. 5 when cols is 9). |
i | int | Outer loop — current row index (1 to rows). |
j | int | Inner loop — current column index (1 to cols). |
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 | Approach | Idea | Best for |
|---|---|---|
| Nested loops + condition | *000*000* fixed 4×9 | Learning and interviews |
| Parameterized rows/cols | mid = (cols + 1) / 2 | Flexible rectangular grids |
| Diagonals only | Drop j == mid check | Pure X without center line |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Walk columns | for (j = 1; j <= cols; j++) |
| Center column | mid = (cols + 1) / 2 |
| Star check | if (i == j || j == mid || i == cols + 1 - j) |
| Print star | Console.Write("*"); |
| Print fill | Console.Write("0"); |
| Program 44 contrast | Centered number diamond with ascending digits — not a star/zero grid |
Same star-and-zero X — different ways to control dimensions and which lines print stars.
i = 1..rowsRows of the grid
j = 1..colsColumns per row
mid = (cols+1)/2Vertical line column
i==j || j==midThree-way star check
Reach for this pattern when teaching 2D grids, diagonal math, and multi-condition cell checks.
Natural follow-up after Program 44 — same nested loops but adds diagonal and center conditions.
Practice i == j and i + j == cols + 1 on paper before coding.
Unlike square patterns, rows and cols can differ — center column needs odd width.
Compare Program 44 (number diamond) and Program 46 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, diagonal conditions, and O(rows×cols) thinking.
Set rows (3–6) and odd column width (7–11), then draw the star-and-zero X in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print a 4×9 star-and-zero X with nested loops and a three-part condition.
rows = 4, cols = 9Hard-coded grid dimensions — ideal for first demos and screenshots.
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();
}
}
}
} When i = 1 and j = 1, i == j is true — prints *. When i = 2 and j = 5, j == 5 hits the center column — prints *. All other cells print 0.
Use rows, cols, and mid instead of hard-coded 4, 9, and 5.
Compute mid and use (cols + 1) - j for the anti-diagonal.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 4, cols = 9;
int mid = (cols + 1) / 2;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
if (i == j || j == mid || i == (cols + 1) - j)
Console.Write("*");
else
Console.Write("0");
}
Console.WriteLine();
}
}
}
} Same inner-loop core as Example 1; mid replaces the literal 5, and (cols + 1) - j replaces 10 - j. Change rows or cols to resize the pattern.
Drop the center-column check for a pure X without the vertical line.
Remove j == mid from the condition — only the two diagonals print stars.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 9; j++)
{
if (i == j || i == 10 - j)
Console.Write("*");
else
Console.Write("0");
}
Console.WriteLine();
}
}
}
} Without the center column, row 4 no longer prints 000***000 — it becomes 000*0*000. Compare with Example 1 to see how one condition changes the shape.
using System; brings in Console. Set loop variables i, j for a 4 × 9 grid.
for (i = 1; i <= rows; i++) and for (j = 1; j <= cols; j++) visit every cell in the grid.
if (i == j || j == mid || i == cols + 1 - j) — true on a diagonal or center column.
Matching cells print "*"; all others print "0".
Console.WriteLine() ends each row after the inner loop finishes.
Every cell visited once — O(rows×cols) time, O(1) extra memory.
i = 3, cols = 9Trace each column j on row 3 — which cells match a diagonal or center condition.
j | Condition | Prints |
|---|---|---|
1 | No | 0 |
2 | No | 0 |
3 | i == j | * |
4 | No | 0 |
5 | j == mid | * |
6 | No | 0 |
7 | i == 10 - j | * |
8 | No | 0 |
9 | No | 0 |
Row 3 output: 00*0*0*00 — stars at columns 3, 5, and 7. Total cells = rows × cols = 36 for a 4×9 grid.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: swap 0 for . or space — see FAQ.
Foundation for X patterns, cross grids, and diagonal-only variants.
Example: continue to Program 46 for the next pattern in the series.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Learn why i == j and i + j == cols + 1 mark the two diagonals.
Example: trace row i = 3 in the walkthrough table.
Rectangular totals make O(rows×cols) concrete for beginners.
Example: count star cells for 4×9 — total grid cells = 36.
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Drop center column, change fill char, or resize rows/cols with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 and each j on paper — watch how one cell can match multiple conditions at intersections.
Small habits that keep number-pattern code clean.
Never hard-code 5 or 10 — use mid and (cols + 1) - j everywhere.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Use odd cols so mid = (cols + 1) / 2 lands on a single center column.
Trace columns j = 1..9 on paper before coding the full 4×9 demo.
Pro Tip: if the output is a vertical list of single squares per line, you almost certainly put WriteLine inside the print loop.
Mistakes that commonly break star-and-zero X patterns.
Each cell lands on its own line — you get a column, not a square.
→ Use Write("*") or Write("0") per cell; WriteLine only after the inner loop.
Using 10 - j breaks when cols changes from 9 to 11 or 7.
→ Always use (cols + 1) - j for the anti-diagonal.
Only checking diagonals gives a pure X — missing the vertical line in the full pattern.
→ Add j == mid where mid = (cols + 1) / 2.
Even cols has no single middle column — mid may not align as expected.
→ Prefer odd column counts (7, 9, 11) for a clear center line.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
One row of stars and zeros — diagonals collapse to corner cells only.
Outer loop never runs — print nothing or show a message.
Even column width has no single middle — center line may look off.
Smaller width — mid = 4, anti-diagonal uses 8 - j.
Convert.ToInt32 throws — use TryParse.
Each cell visited once — total work grows as rows × cols.
Try these variations to lock in the pattern.
j == mid from the condition. or space instead of 0* when i == j || j == mid || i == cols + 1 - j; else print 0.Console.Write stays on the line; WriteLine advances — mix them carefully.cols for a clear center column; compute mid = (cols + 1) / 2.rows × cols grid has rows × cols cells — each visited exactly once.Quick Takeaway: nested loops over i, j, three-way check prints *, else 0, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(rows × cols) | O(1) |
| Smaller demo (Example 3) | O(rows × cols) | O(1) |
The star-and-zero X pattern is a compact nested-loop lesson: visit every cell in a rectangular grid and use a three-part condition to print * or 0. Master the fixed 4×9 version, then try parameterized dimensions and the diagonals-only variant.
Practice the three examples above, then continue to Program 46 for the next pattern in the series.
Diagonals use i == j and i == cols + 1 - j — add j == mid for the center column and prefer odd column width.
for (i = 1; i <= rows; i++) and for (j = 1; j <= cols; j++)if (i == j || j == mid || i == cols + 1 - j)"*" on match, "0" elsewheremid = (cols + 1) / 2 for center columncols for a clear vertical lineWriteLine inside the inner cell loop10 - j when cols can changej == mid if you want the center columni = 3 before codingPrint the pattern the beginner-friendly way.
* on X + center
DefinitionRows i = 1..rows
CodeColumns j = 1..cols
Codei==j || j==mid
LogicO(rows×cols)
AnalysisPrint * 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.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful