Shape Rule
Mirror diagonals
Row i prints the digit i on the left diagonal and again on the right diagonal — an inverse-V mirror shape.

Program 57 prints a diagonal mirror number pyramid: each row shows the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. A natural step after Program 56’s centered palindromic pyramid. This tutorial covers conditional printing, two inner loops per row, a live preview, worked C# examples, edge cases, and complexity.
Mirror diagonals
Row i prints the digit i on the left diagonal and again on the right diagonal — an inverse-V mirror shape.
i = 1..rows
for (i = 1; i <= rows; i++) picks the current row and the digit to place on both diagonals.
j = rows..1
if (i == j) Console.Write(j) else space — scans from the right edge inward.
k = 2..rows
if (i == k) Console.Write(k) else space — mirrors the left half from column 2 onward.
digit or space
Every column position gets either the row digit or a single space — no other characters.
Complexity
Each row scans about 2×rows-1 positions — total work grows as O(n²).
A diagonal mirror number pyramid prints the row number on two mirror diagonals with spaces everywhere else. With rows = 5, you get 1, 2 2, 3 3, and so on — forming an inverse-V shape.
In C# use an outer loop for rows, then two inner loops: the first scans j = rows..1 for the left diagonal, the second scans k = 2..rows for the right diagonal, printing the digit only when i == j or i == k.
It bridges Program 56’s palindromic rows to conditional diagonal placement — combining if checks with two inner loops per row.
j = rows..1, print when i == j.
k = 2..rows, print when i == k.
Program 56 prints palindromic rows; Program 57 prints the row digit twice on mirror diagonals.
Follow Program 56; continue to Program 58 next.
In short: outer i = 1..rows, left loop j = rows..1, right loop k = 2..rows, print digit or space, then WriteLine().
Given row count rows = 5, print a diagonal mirror number pyramid — row i shows digit i on left and right diagonals with spaces elsewhere.
// rows = 5
// 1
// 2 2
// 3 3
// 4 4
//5 5 | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height — bottom row has rows on both diagonals. |
i (outer) | int | Current row index — runs 1 to rows. |
j (left) | int | Scans rows..1 — prints digit when i == j. |
k (right) | int | Scans 2..rows — prints digit when i == k. |
| Positions per row | int | rows + (rows - 1) = 2×rows - 1 character slots. |
| Digits per row | int | Exactly 2 (except row 1 when right loop is empty for rows = 1). |
for i from 1 to rows:
for j from rows down to 1:
print i if i == j else space
for k from 2 to rows:
print i if i == k else space
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Left j = rows..1, right k = 2..rows | Learning and interviews |
| Conditional print | if (i == j) digit else space | Diagonal placement drills |
| User-input rows | int.TryParse(...) | Flexible pyramid size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Character swap | Replace digit with * for an X-shape | Visual debugging |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; 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 56 contrast | Program 56 uses palindromic rows; Program 57 uses mirror diagonals |
Same diagonal mirror pyramid — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
TryParseRead row count from console
rows = 3Quick dry-run on paper
j = rows..1Print when i == j
k = 2..rowsPrint when i == k
Reach for this pattern when teaching conditional printing, diagonal placement, and combining two inner loops per row.
Natural follow-up after Program 56’s palindromic pyramid — introduces conditional diagonal placement.
Each row places digits on mirror diagonals — good bridge to matrix diagonal problems.
Left and right halves with if checks — concrete nested-loop practice.
Program 58 extends this diagonal logic to a full diamond — compare after mastering this pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in conditional printing, mirror diagonals, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered diagonal mirror number pyramid 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 diagonal mirror pyramid with five rows — left and right inner loops with conditional printing per row.
rows = 5Hard-coded row count — scan left diagonal j = rows..1, then right diagonal k = 2..rows, printing digit or space.
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();
}
}
}
} When i = 3, the left loop prints spaces then 3 at j = 3; the right loop prints spaces then 3 at k = 3 — output 3 3. When i = 1, only the left loop places a digit; the right loop is all spaces.
Read row count 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--)
{
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();
}
}
}
} Same conditional diagonal logic as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace left and right diagonal loops before scaling to 5 rows.
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--)
{
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();
}
}
}
} With only three rows you can trace every i == j and i == k check on paper before running the full rows = 5 demo.
int rows = 5; controls pyramid height and the maximum digit printed.
for (j = rows; j >= 1; j--) — print digit when i == j, else space.
for (k = 2; k <= rows; k++) — print digit when i == k, else space.
Call Console.WriteLine() after both inner loops finish — one mirror-diagonal row complete.
Each row scans 2×rows-1 positions — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s left diagonal hit, right diagonal hit, and full line output.
i | Left hit (j) | Right hit (k) | Row output |
|---|---|---|---|
1 | j = 1 | (none) | 1 |
2 | j = 2 | k = 2 | 2 2 |
3 | j = 3 | k = 3 | 3 3 |
4 | j = 4 | k = 4 | 4 4 |
5 | j = 5 | k = 5 | 5 5 |
Row i always prints exactly two digits (one per diagonal) when rows > 1 — spaced across 2×rows-1 character positions.
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.
Each row instantly forms an inverse-V — two digits on mirror diagonals make the shape obvious.
Conditional digit-or-space printing teaches real console alignment — not abstract loop drill.
Swap digits for * to get an X-shape, or extend to Program 58’s full diamond.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 shows 2 2 with one space between the digits.
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 pyramid 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.
Console.Write(i) with Console.Write('*')j = rows..1, print when i == j. Right: k = 2..rows, print when i == k.Console.Write stays on the line; WriteLine advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single 1 on the left diagonal.i scans 2×rows-1 positions — total work grows as O(n²) for n rows.Quick Takeaway: left j = rows..1, right k = 2..rows, digit or space, 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 pyramid is a natural follow-up to Program 56: each row places the row digit on two mirror diagonals with conditional printing. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 58 for the full diagonal mirror diamond.
Row i prints two copies of digit i — one on each mirror diagonal across 2×rows-1 positions.
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 mirror-diagonal pyramid the beginner-friendly way.
digit i twice per row
Definitionj = rows..1
Codek = 2..rows
Codei == j or i == k
LogicO(n²) time
AnalysisEach row prints the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. Total positions per row = 2×rows-1.
Move on to the full diagonal mirror diamond in the C# number-pattern series.
12 people found this page helpful