Shape Rule
Mirrored diagonals
Row i prints i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.

Program 53 prints a mirror diagonal number pattern: each row shows the row number on the main diagonal (left) and on a mirrored diagonal (right), forming a symmetric V-shape — a natural step after Program 52’s palindromic pyramid. This tutorial covers two inner loops with i == j and i == k conditions, a live preview, worked C# examples, edge cases, and complexity.
Mirrored diagonals
Row i prints i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.
i = 1..rows
for (i = 1; i <= rows; i++) picks the current row index.
j = 1..rows
Console.Write(i == j ? j.ToString() : " ") — print the digit only on the main diagonal.
k = rows-1..1
Console.Write(i == k ? k.ToString() : " ") — mirrored diagonal; skipping the center column avoids duplication.
rows = 3..9
Pick row count and draw the V-shaped mirror diagonal pattern in the browser.
Complexity
Each row prints about 2n-1 characters — total work grows as O(n²).
A mirror diagonal number pattern prints row i with the digit i on the main diagonal and again on a mirrored diagonal — spaces fill the gaps to form a V-shape. With rows = 5, you get 1 1, 2 2, 3 3, 4 4, 5.
In C# use an outer loop for rows, then two inner loops: left half with i == j, right mirrored half with i == k, printing spaces elsewhere before WriteLine().
It bridges Program 52’s palindromic rows to conditional diagonal placement — combining nested loops with i == j logic.
i == j prints the row digit on the main diagonal.
i == k mirrors the digit on the opposite diagonal.
Program 52 uses m++/m-- for palindromic rows; Program 53 uses spacing and conditions.
Follow Program 52; continue to Program 54 next.
In short: outer i = 1..rows, left loop j = 1..rows with i == j, right loop k = rows-1..1 with i == k, else space, then WriteLine().
Given row count rows = 5, print a mirror diagonal number pattern — row i shows digit i on both diagonals with spaces between.
// rows = 5
//1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
rows | int | How many V-shaped rows to print. |
i (outer) | int | Current row index — runs from 1 to rows. |
j (left) | int | Scans columns 1..rows; prints digit when i == j. |
k (right) | int | Scans columns rows-1..1; prints digit when i == k. |
| Cell output | string | Digit when condition matches; otherwise a space. |
| Row width | int | About 2n-1 characters per row. |
for i from 1 to rows:
for j from 1 to rows:
print digit if i == j else space
for k from rows - 1 down to 1:
print digit if i == k else space
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Left i == j, right i == k with spaces elsewhere | Learning and interviews |
| Ternary operator | i == j ? j.ToString() : " " | Compact one-liners |
| User-input rows | int.TryParse(...) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Full X pattern | i == j || i + j == rows + 1 in one loop | Extension after mastering V-shape |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Left half | for (j = 1; j <= rows; j++) Console.Write(i == j ? j.ToString() : " "); |
| Right half | for (k = rows - 1; k >= 1; k--) Console.Write(i == k ? k.ToString() : " "); |
| End row | Console.WriteLine(); |
| Skip center duplicate | Right loop starts at rows - 1, not rows |
| Program 52 contrast | Program 52 uses palindromic m++/m--; Program 53 uses diagonal conditions |
Same V-shape — 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
i == jMain diagonal digit placement
i == kMirrored diagonal digit placement
Reach for this pattern when teaching conditional diagonal placement, mirrored halves, and spacing in console output.
Natural follow-up after Program 52’s palindromic pyramid — introduces i == j diagonal conditions.
Each row places digits only where indices match — good bridge to matrix and grid problems.
Each row scans about 2n-1 positions — classic nested-loop O(n²) complexity.
Program 54 mirrors this V-shape downward to form a full diamond — compare the two next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in diagonal conditions, mirrored halves, and O(n²) thinking.
Choose row count between 3 and 9 and draw the mirror diagonal number pattern 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 five rows of the mirror diagonal V-shape with conditional digit placement on both diagonals.
rows = 5Hard-coded row count — print digit when i == j or i == k, otherwise print a 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 = 1; j <= rows; j++)
Console.Write(i == j ? j.ToString() : " ");
for (int k = rows - 1; k >= 1; k--)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
} When i = 3, the left loop prints spaces until j = 3, then the right loop prints spaces until k = 3 — output 3 3. When i = 5, only the center column gets a digit because both diagonals meet at the bottom tip.
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 = 1; j <= rows; j++)
Console.Write(i == j ? j.ToString() : " ");
for (int k = rows - 1; k >= 1; k--)
Console.Write(i == k ? k.ToString() : " ");
Console.WriteLine();
}
}
}
} Same diagonal two-loop core 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 conditions 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 = 1; j <= rows; j++)
Console.Write(i == j ? j.ToString() : " ");
for (int k = rows - 1; k >= 1; k--)
Console.Write(i == k ? k.ToString() : " ");
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 how many V-shaped lines print.
for (j = 1; j <= rows; j++) — print digit when i == j, else space.
for (k = rows - 1; k >= 1; k--) — print digit when i == k, else space.
Console.WriteLine() after both inner loops finish the current line.
Each row prints about 2n-1 characters — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s left diagonal position, right diagonal position, and full line output.
i | Left (j) | Right (k) | Row output |
|---|---|---|---|
1 | j = 1 | k = 1 | 1 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 | (none — center tip) | 5 |
Row 5 prints only one digit because the right loop starts at rows - 1, avoiding a duplicate center column.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Each row scans a fixed-width grid with conditional digit placement.
Example: trace row i = 3 in the walkthrough table.
Each row mirrors digits on two diagonals — good bridge to matrix indexing.
Example: row 5 ends with a single center digit 5 at the V tip.
Practice Write vs WriteLine with multiple values per row.
Example: put WriteLine inside the inner loop by mistake.
Each row prints about 2n-1 characters — links loops to grid traversal.
Example: 10 rows scan about 19 characters on the widest line.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows scan about 9 characters per line on average.
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.
Wrong conditions show up immediately as misaligned diagonals.
Each row uses real diagonal logic — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 on paper — watch both loops print 3 at column 3 with spaces elsewhere.
Small habits that keep number-pattern code clean.
Scan all columns in the left half — print digit only when i == j.
Avoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Start the right loop at rows - 1 to skip duplicating the center column.
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 mirror diagonal number patterns.
Each character lands on its own line — you get a column, not a V-shape.
→ Use Console.Write(...) for digits and spaces; WriteLine only after both inner loops.
Starting the right loop at k = rows duplicates the center digit on the bottom row.
→ Use for (k = rows - 1; k >= 1; k--) — skip the center column.
Digits appear everywhere instead of on the diagonals only.
→ Print the digit when i == j (or i == k), not when they differ.
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 does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Five rows ending with a single center 5 — good for dry-runs.
Convert.ToInt32 throws — use TryParse.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
m++/m-- rowsi == j diagonal conditionsi == j || i + j == rows + 1 in one column loopj = 1..rows with i == j. Right: k = rows-1..1 with i == k. Else print a space.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.2n-1 characters — total work is O(n²) for n rows.Quick Takeaway: outer i = 1..rows, left i == j, right i == k, else space, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Characters per row | About 2n-1 | No storage beyond loop counters |
The mirror diagonal number pattern is a natural follow-up to Program 52: conditional digit placement on mirrored diagonals with spaces elsewhere. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 54 to mirror this V-shape into a full diamond.
Row i prints digit i on both diagonals — left with i == j, right with i == k.
for (j = 1; j <= rows; j++) Console.Write(i == j ? j.ToString() : " ");for (k = rows - 1; k >= 1; k--) Console.Write(i == k ? k.ToString() : " ");rows - 1 to skip center duplicationWriteLine() after both inner loopsint.TryParse for user inputk = rows — duplicates the center digiti != j — fills the whole row with numbersWriteLine inside either inner looprows = 3 dry-run before coding rows = 5Print the V-shape the beginner-friendly way.
Digit on both diagonals per row
Definitioni == j
Codei == k
Codek = rows - 1..1
LogicO(n²) time
AnalysisEach row prints the row number on the main diagonal (left) and on a mirrored diagonal (right) using i == j and i == k. Row 3 shows 3 on both sides — about 2n-1 characters per row, so O(n²) total.
Mirror this V-shape downward to form a full diamond in the next tutorial.
12 people found this page helpful