Shape Rule
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.

Program 58 prints a diagonal mirror number diamond: the top half grows from 1 to rows like Program 57, then a second outer loop mirrors back down to 1. This tutorial covers top/bottom halves, conditional diagonal printing, a live preview, worked C# examples, edge cases, and complexity.
Full diamond
Top half: row i prints digit i on mirror diagonals. Bottom half: same logic with i counting down.
i = 1..rows
for (i = 1; i <= rows; i++) — same pyramid half as Program 57.
i = rows-1..1
for (i = rows - 1; i >= 1; i--) — mirrors the top half without repeating the peak row.
j = rows..1
if (i == j) Console.Write(j) else space — reused in both outer loops.
k = 2..rows
if (i == k) Console.Write(k) else space — mirrors the left half from column 2 onward.
Complexity
2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).
A diagonal mirror number diamond extends Program 57’s pyramid: print the top half from 1 to rows, then mirror back down with a second outer loop from rows-1 to 1. With rows = 5, you get nine lines — peak at row 5, then symmetric descent to a single 1.
Each row reuses Program 57’s inner loops: left diagonal j = rows..1, right diagonal k = 2..rows, printing the digit only when i == j or i == k.
It bridges Program 57’s single pyramid to full symmetry — one extra outer loop turns a half-pattern into a complete diamond.
i = 1..rows — pyramid grows upward.
i = rows-1..1 — mirror without repeating peak.
Program 57 is the top half only; Program 58 adds the mirrored bottom loop.
Follow Program 57; continue to Program 59 next.
In short: top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic per row, then WriteLine().
Given row count rows = 5, print a diagonal mirror number diamond — top half 1..rows, bottom half rows-1..1, with mirror diagonals on every line.
// rows = 5
// 1
// 2 2
// 3 3
// 4 4
// 5 5
// 4 4
// 3 3
// 2 2
// 1 | Item | Type | Description |
|---|---|---|
rows | int | Half-height — diamond has 2×rows-1 total lines. |
i (top outer) | int | Runs 1 to rows — builds the upper half. |
i (bottom outer) | int | Runs rows-1 down to 1 — mirrors without repeating peak. |
j (left) | int | Scans rows..1 — prints digit when i == j. |
k (right) | int | Scans 2..rows — prints digit when i == k. |
| Total lines | int | rows + (rows - 1) = 2×rows - 1. |
for i from 1 to rows:
print row i with left and right diagonal logic
for i from rows - 1 down to 1:
print row i with same inner loop logic | Approach | Idea | Best for |
|---|---|---|
| Two outer loops | Top 1..rows, bottom rows-1..1 | Learning and interviews |
| Reuse inner logic | Same j and k loops in both halves | DRY diamond patterns |
| User-input rows | int.TryParse(...) | Flexible diamond size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Character swap | Replace digit with * for an X-diamond | Visual debugging |
| Goal | Pattern |
|---|---|
| Top outer loop | for (i = 1; i <= rows; i++) |
| Bottom outer loop | for (i = rows - 1; i >= 1; i--) |
| Left diagonal | for (j = rows; j >= 1; j--) if (i == j) Console.Write(j); else Console.Write(" "); |
| Right diagonal | for (k = 2; k <= rows; k++) if (i == k) Console.Write(k); else Console.Write(" "); |
| End row | Console.WriteLine(); |
| Program 57 contrast | Program 57 is top half only; Program 58 adds bottom mirror loop |
Same diagonal mirror diamond — three ways to set row count and trace the logic.
rows = 5Hard-coded half-height for demos
TryParseRead row count from console
rows = 35-line diamond dry-run
i = 1..rowsProgram 57 pyramid logic
i = rows-1..1Mirror without peak repeat
Reach for this pattern when teaching symmetry, mirroring loops, and extending a half-pattern into a full diamond.
Natural follow-up after Program 57’s pyramid — one extra outer loop completes the diamond.
Top and bottom halves share inner logic — good bridge to palindrome and mirror problems.
Separate top and bottom boundaries — concrete loop-boundary practice.
Compare this hollow diamond with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small extension that locks in mirroring, symmetry, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered diagonal mirror number diamond in the browser.
Three complete C# programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print a full diagonal mirror diamond with half-height five — top loop 1..rows, bottom loop rows-1..1.
rows = 5Hard-coded half-height — print the top pyramid, then mirror with a second outer loop using the same inner diagonal logic.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= 1; j--)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (int k = 2; k <= rows; k++)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows; j >= 1; j--)
{
if (i == j)
Console.Write(j);
else
Console.Write(" ");
}
for (int k = 2; k <= rows; k++)
{
if (i == k)
Console.Write(k);
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
} The first outer loop prints rows 1 through 5 (Program 57 logic). The second outer loop prints rows 4 down to 1 — reusing the same inner loops so the bottom half mirrors the top without repeating row 5.
Read half-height from the console with safe parsing.
Read rows from the console with int.TryParse — reject invalid input gracefully.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
} Same top-and-bottom outer loops as Example 1; only the source of rows changes from a literal to user input.
Smaller half-height for quick tracing on paper or in interviews.
rows = 3Use rows = 3 for a 5-line diamond — trace both outer loops before scaling to 5.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
for (int i = rows - 1; i >= 1; i--)
{
for (int j = rows; j >= 1; j--)
Console.Write(i == j ? j.ToString() : " ");
for (int k = 2; k <= rows; k++)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
} With half-height 3 you get 5 total lines — enough to trace top loop, peak row, and bottom mirror on paper before the full demo.
int rows = 5; is half-height — the diamond prints 2×rows-1 = 9 lines.
for (i = 1; i <= rows; i++) — Program 57 pyramid logic with left and right diagonal inner loops.
for (i = rows - 1; i >= 1; i--) — same inner loops, counting down to avoid repeating the peak row.
Left j = rows..1, right k = 2..rows — print digit when i == j or i == k, else space.
2×rows-1 lines total — O(n²) time, O(1) extra memory.
rows = 5Trace each line’s half (top or bottom), row index, and diagonal hits — nine lines total.
| Line | Half | i | Left hit | Right hit |
|---|---|---|---|---|
| 1 | Top | 1 | j=1 | (none) |
| 2 | Top | 2 | j=2 | k=2 |
| 3 | Top | 3 | j=3 | k=3 |
| 4 | Top | 4 | j=4 | k=4 |
| 5 | Top (peak) | 5 | j=5 | k=5 |
| 6 | Bottom | 4 | j=4 | k=4 |
| 7 | Bottom | 3 | j=3 | k=3 |
| 8 | Bottom | 2 | j=2 | k=2 |
| 9 | Bottom | 1 | j=1 | (none) |
The bottom loop starts at rows-1 so line 5 (peak) is not printed twice — total lines = 2×rows-1.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Two inner loops with conditional printing — classic diagonal placement drill.
Example: trace each row in the walkthrough table — left hit, right hit.
Each row mirrors digits on two diagonals — compare with Program 53’s single diagonal V-shape.
Example: row 5 prints 5 at column 5 and again at column 9.
Practice Write vs WriteLine with digit-or-space decisions per column.
Example: put WriteLine inside the inner loop by mistake.
Starting the right loop at 2 avoids a third digit at the center — keeps exactly two prints per row.
Example: try k = 1 and see the center digit triple on some rows.
Each row scans about 2n positions — makes O(n²) concrete for beginners.
Example: row 5 with rows = 5 scans 9 character slots — see the walkthrough table.
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 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.
The full diamond is instantly recognizable — top half grows, bottom half mirrors symmetrically.
Conditional digit-or-space printing teaches real console alignment — not abstract loop drill.
Swap digits for * to get an X-diamond, or try fixed-width formatting for rows beyond 9.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — 5 lines total, peak at row 3, then mirror back to 1.
Small habits that keep number-pattern code clean.
Print the digit when i == j; otherwise print a single space.
Avoid crashes when the user types letters instead of a number.
Only call WriteLine() after both inner loops finish the row.
Use for (k = 2; k <= rows; k++) so the center position is not duplicated.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break diagonal mirror number diamond patterns.
Each digit lands on its own line — you get a column, not a pyramid.
→ Use Console.Write(j) or Console.Write(" "); WriteLine only after both inner loops.
Starting at k = 1 can print a third digit at the center — row looks crowded.
→ Use for (k = 2; k <= rows; k++) — mirror from column 2 onward.
Using j == rows instead of i == j places digits on the wrong diagonal.
→ Always compare the outer row index i with the inner loop variable j or k.
All numbers print on one long line without row breaks.
→ Add Console.WriteLine() after both inner loops complete.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop (k = 2..1) does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Bottom row has two copies of 5 across 9 positions — good for dry-runs before scaling up.
Convert.ToInt32 throws — use TryParse.
Row 9 scans 17 character positions (2×9-1) — total work grows as O(n²).
Try these variations to lock in the pattern.
rows-1..1 for the full diamondrows and see the middle row print twicerows-1Console.Write('*')i = 1..rows. Bottom: i = rows-1..1. Same inner diagonal logic in both.Console.Write stays on the line; WriteLine advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints one line — bottom loop does not run.2×rows-1 lines, each scanning about 2×rows-1 positions — total work grows as O(n²).Quick Takeaway: top 1..rows, bottom rows-1..1, same inner diagonal logic, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Digits on row i | 2i - 1 | No storage beyond loop counters |
The diagonal mirror number diamond is a natural follow-up to Program 57: one extra outer loop mirrors the pyramid into a full symmetric diamond. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 59 for the next pattern in the series.
Total output is 2×rows-1 lines — peak at row rows, then mirrored descent to 1.
for (j = rows; j >= 1; j--) with if (i == j)for (k = 2; k <= rows; k++) with if (i == k)WriteLine() after both inner loopsint.TryParse for user inputk = 1 — can triple-print at centerj == rows instead of i == j — wrong diagonalWriteLine inside any inner looprows = 3 dry-run before coding rows = 5Print the full mirror-diagonal diamond the beginner-friendly way.
2×rows-1 lines
Definitionj = rows..1
Codek = 2..rows
Codei == j or i == k
LogicO(n²) time
AnalysisPrint the Program 57 pyramid for the top half, then mirror with for (i = rows-1; i >= 1; i--). Total lines = 2×rows-1 — each row scans about 2×rows-1 positions.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful