Shape Rule
Full diamond
Top half i = k..1, bottom half i = 2..k — complete symmetric diamond.

Program 47 prints a full concentric number diamond: top half peels from k to 1, then the bottom half mirrors from 2 back to k — a natural step after Program 46’s top-half square. This tutorial covers two outer loops with j > i logic, a live preview, worked C# examples, edge cases, and complexity.
Full diamond
Top half i = k..1, bottom half i = 2..k — complete symmetric diamond.
i = k..1
for (i = k; i >= 1; i--) prints layers from outside down to center.
i = 2..k
for (i = 2; i <= k; i++) mirrors rows back out — skip i = 1 (already printed).
j > i
if (j > i) print j; else print i — picks outer or current layer value.
k = 3..7
Pick outer value k and draw the full concentric diamond in the browser.
Complexity
k top rows + k - 1 bottom rows × 2k - 1 columns — total ≈ (2k-1)² prints.
A concentric number diamond prints layers that decrease toward the center, then mirror back out to form a complete symmetric shape. With k = 5, the grid is 9 × 9 — the center cell is 1.
In C# two outer loops handle top (i = k..1) and bottom (i = 2..k) halves; each row uses two inner loops and the j > i rule.
It extends Program 46 with a bottom-half loop — the key step from half-pattern to full diamond symmetry.
Top k..1, bottom 2..k.
Two inner loops per row.
Program 46 is top half only; Program 47 adds the bottom mirror.
Follow Program 46; continue to Program 48 next.
In short: top loop i = k..1, bottom loop i = 2..k, cell rule j > i ? j : i, then WriteLine().
Given outer value k = 5, print a full concentric number diamond — top half peels to 1, bottom half mirrors back out.
// k = 5 (9x9)
//5 5 5 5 5 5 5 5 5
//5 4 4 4 4 4 4 4 5
//5 4 3 3 3 3 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 2 1 2 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 3 3 3 3 4 5
//5 4 4 4 4 4 4 4 5
//5 5 5 5 5 5 5 5 5 | Item | Type | Description |
|---|---|---|
k | int | Outer (maximum) number — also sets row count and half-width. |
i (top) | int | Top outer loop — layer value from k down to 1. |
i (bottom) | int | Bottom outer loop — layer value from 2 up to k. |
j | int | Inner loop — column index for left (k..1) or right (2..k) half. |
| Grid size | int | 2 × k - 1 rows and columns (9 when k = 5). |
for i from k down to 1: // top half
for j from k down to 1: print j if j > i else i
for j from 2 to k: print j if j > i else i
print newline
for i from 2 to k: // bottom half
for j from k down to 1: print j if j > i else i
for j from 2 to k: print j if j > i else i
print newline | Approach | Idea | Best for |
|---|---|---|
| Two outer loops + ternary | 9×9 diamond for k = 5 | Learning and interviews |
| User-input k | int.TryParse(...) | Flexible outer value |
| Ternary operator | j > i ? j : i | Compact one-liner per cell |
| Goal | Pattern |
|---|---|
| Top half | for (i = k; i >= 1; i--) |
| Bottom half | for (i = 2; i <= k; i++) |
| Left half | for (j = k; j >= 1; j--) |
| Right half | for (j = 2; j <= k; j++) |
| Cell rule | if (j > i) Console.Write(j + " "); else Console.Write(i + " "); |
| Ternary form | Console.Write((j > i ? j : i) + " "); |
| Grid size | 2 × k - 1 rows and columns |
| Program 46 contrast | Top-half square only — Program 47 adds bottom mirror loop |
Same full diamond — different ways to set k and trace the two outer loops.
i = k..1Peel toward center
i = 2..kMirror back out
j = k..1Descending columns
j = 2..kMirror without center dup
j > i ? j : iOuter or layer value
Reach for this pattern when teaching symmetric output, layer logic, and dual inner loops.
Natural follow-up after Program 46 — adds the bottom-half outer loop for a complete diamond.
Top (k..1) and bottom (2..k) teach full vertical symmetry.
j > i selects which concentric ring each cell belongs to.
Compare Program 46 (top half) and Program 48 (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, symmetry, and O(k²) thinking.
Choose outer value k between 3 and 7 and draw the full concentric diamond 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 full concentric number diamond with k = 5 using two outer loops and the j > i rule.
k = 5Hard-coded outer value — top half then bottom half for a complete 9×9 diamond.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k = 5;
int i, j;
for (i = k; i >= 1; i--)
{
for (j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
for (i = 2; i <= k; i++)
{
for (j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
}
}
} The first outer loop prints rows i = 5..1 (top half). The second prints i = 2..5 (bottom half) — row i = 1 is skipped because it was already the center row.
Read outer value k from the console instead of hard-coding 5.
Read k from the console with safe parsing — both halves adjust automatically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k;
Console.Write("Enter k: ");
if (!int.TryParse(Console.ReadLine(), out k) || k <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = k; i >= 1; i--)
{
for (int j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (int j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
for (int i = 2; i <= k; i++)
{
for (int j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (int j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
}
}
} Same two-pass structure as Example 1; only the source of k changes. Grid size becomes 2k - 1 rows and columns.
Smaller outer value for quick tracing — 5 rows total (3 top + 2 bottom).
k = 3Use k = 3 to trace both outer loops quickly on paper or in interviews.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k = 3;
for (int i = k; i >= 1; i--)
{
for (int j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (int j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
for (int i = 2; i <= k; i++)
{
for (int j = k; j >= 1; j--)
Console.Write((j > i ? j : i) + " ");
for (int j = 2; j <= k; j++)
Console.Write((j > i ? j : i) + " ");
Console.WriteLine();
}
}
}
} Five rows total — three from the top loop, two from the bottom (skipping center duplicate). Easy to dry-run before scaling to k = 5.
using System; brings in Console. Set k = 5 as the outer value and row/layer count.
for (i = k; i >= 1; i--) walks layers from outside down to center row.
If j > i print j; else print i — builds descending left side.
for (i = 2; i <= k; i++) prints rows back out — skips i = 1 (center already done).
Console.WriteLine() after both inner loops finish each row.
Grid size 2k - 1 × 2k - 1 — O(k²) time, O(1) extra memory.
i = 3, k = 5Trace left-half columns j on row 3 — which value prints for each cell.
j | j > i? | Prints |
|---|---|---|
5 | Yes | 5 |
4 | Yes | 4 |
3 | No | 3 (i) |
2 | No | 3 (i) |
1 | No | 3 (i) |
Left half of center row (i = 1): 5 4 3 2 1. Right half mirrors to 2 3 4 5 — full center row: 5 4 3 2 1 2 3 4 5. Bottom loop then prints rows i = 2..5 to complete the 9×9 diamond.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change k to 3 for a quick trace — see Example 3.
Foundation for concentric layers, symmetric grids, and peel-down patterns.
Example: continue to Program 48 for the next pattern in the series.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Two inner loops teach left-right mirroring without string reversal.
Example: trace row i = 3 in the walkthrough table.
2k - 1 rows × 2k - 1 columns makes O(k²) concrete.
Example: count cells for k = 5 — 9 × 9 = 81 prints.
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.
Change k, use ternary form, or trace with k = 3 for quick dry-runs.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 on paper — watch how j > i switches from outer values to the current layer.
Small habits that keep number-pattern code clean.
Never hard-code 5 in loop bounds — use k everywhere.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Left half uses j = k..1; right half uses j = 2..k — do not repeat j = 1 on the right.
Trace all three rows on paper before coding the full k = 5 demo.
Pro Tip: if the output is a vertical list of single numbers per line, you almost certainly put WriteLine inside the print loop.
Mistakes that commonly break concentric number diamond patterns.
Each cell lands on its own line — you get a column, not a diamond.
→ Use Write((j > i ? j : i) + " ") per cell; WriteLine only after both inner loops.
Starting the right half at j = 1 duplicates the center digit on every row.
→ Use for (j = 2; j <= k; j++) for the mirror half.
Only the top loop runs — output stops at the center row like Program 46.
→ Add for (i = 2; i <= k; i++) with the same inner loops after the top half.
k = 1 prints a single 1; k = 2 gives a minimal 3×3 diamond.
→ Validate k >= 2 for interactive programs expecting a visible pattern.
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 on one line — no layers to peel.
Outer loop never runs — print nothing or show a message.
k < 0Treat as invalid; re-prompt instead of silent empty output.
3×3 grid — 2 2 2, 2 1 2, 2 2 2.
Convert.ToInt32 throws — use TryParse.
Each row prints 2k - 1 cells — total work grows as k².
Try these variations to lock in the pattern.
i = 2, not i = 1i = k..1. Bottom: i = 2..k (skip center duplicate). Same inner loops and cell rule in both.Console.Write stays on the line; WriteLine advances — call it only after both inner loops finish.k > 0 for interactive programs; k = 1 prints a single 1.2k - 1 rows × 2k - 1 columns — total prints ≈ (2k - 1)².Quick Takeaway: top loop i = k..1, bottom loop i = 2..k, cell rule j > i ? j : i, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(k²) | O(1) |
| Smaller demo (Example 3) | O(k²) | O(1) |
The concentric number diamond extends Program 46 with a bottom-half outer loop: top (i = k..1) plus bottom (i = 2..k) using the same j > i cell rule. Master the fixed-k version, then try user input and the compact k = 3 trace.
Practice the three examples above, then continue to Program 48 for the next pattern in the series.
Two outer loops — top k..1, bottom 2..k — skip i = 1 in the bottom loop to avoid duplicating the center row.
for (i = k; i >= 1; i--), Bottom: for (i = 2; i <= k; i++)j = k..1, Right: j = 2..k per rowj if j > i, else ii = 1 in bottom loop (center already printed)k ≥ 2 for interactive programsWriteLine inside the inner cell loopi = 1 — duplicates center rowk = 3 dry-run before coding k = 5Print the pattern the beginner-friendly way.
Two outer loops
Definitioni = k..1
Codei = 2..k
Codej > i ? j : i
Logic(2k-1)² cells
AnalysisPrint top half with for (i = k; i >= 1; i--), then bottom half with for (i = 2; i <= k; i++). Each cell: j > i ? j : i. Grid size = 2k - 1 rows and columns.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful