Home C# Number Patterns Program 47 C# Concentric Number Diamond Pattern Definition
What Is This Pattern? A concentric number diamond is Program 46’s square mirrored top-to-bottom: layers from k down to 1, then back out to k.
Rule: top i=k..1, bottom i=2..k; cell = (j > i ? j : i)
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 ← k = 5 Same row logic as Program 46 (left k..1, right 2..k). Add a second outer loop i = 2..k to complete the diamond.
Approach
How to Solve It Reuse the square’s row builder twice — once going in, once going out.
Method Idea Best for Two-half diamond Top k..1, bottom 2..k, same cell rule Learning, interviews, exams Input k Same logic with a user-chosen peak Practice / demos
Pseudocode for i from k down to 1:
print left half j = k..1 and right half j = 2..k
(value = j if j > i else i)
for i from 2 to k:
print the same row rule again
print newline after each row Cheat sheet Goal Pattern Top half for (i = k; i >= 1; i--)Bottom half for (i = 2; i <= k; i++)Left / right j = k..1 / j = 2..kCell value Console.Write((j > i ? j : i) + " ");End the row Console.WriteLine();
Write vs WriteLine API Effect Use for Console.WriteStays on the same line Each number plus a trailing space Console.WriteLineEnds the current line After both half-loops finish a row
Try it
Live Preview Change k and the concentric diamond updates instantly.
Trace
Worked Walkthrough — k = 3 Top half walks in; bottom half walks out (skip repeating the center).
Half iPrinted row Top 33 3 3 3 3Top 23 2 2 2 3Top 13 2 1 2 3Bottom 23 2 2 2 3Bottom 33 3 3 3 3
Total lines = 2k - 1. Cells for k = 5 = 9 × 9 = 81.
Code
C# Programs Three complete programs: fixed k = 5, user-input k, and a compact k = 3 demo. Use View Output for sample results.
1. Fixed k 2. User input 3. Compact demo Example 1 — Fixed k = 5 Top half then bottom half — same row builder twice.
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();
}
}
}
} 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 How It Works 1. Top half. i runs from 5 down to 1 — same concentric rows as Program 46.
2. Bottom half. i runs from 2 up to 5 — mirrors outward without repeating the center.
3. Cell rule. Every print uses j > i ? j : i on left k..1 and right 2..k.
Example 2 — User Input (k) Read k; both halves and width scale automatically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k, i, j;
Console.Write("Enter k: ");
if (!int.TryParse(Console.ReadLine(), out k) || k < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
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();
}
}
}
} How It Works 1. Validate k. TryParse rejects non-numeric input; require k >= 1.
2. Same diamond. Size becomes (2k - 1) × (2k - 1).
Example 3 — Compact k = 3 A smaller fixed demo — five lines, easy to check by hand.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k = 3;
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();
}
}
}
} 3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3 How It Works 1. Same rules. Grow in, then out; cell value is always j > i ? j : i.
2. Quick check. Center row is 3 2 1 2 3; first and last rows are all 3s.
Edge Cases & Pitfalls Check these before calling the solution done.
i = 1 twice Bottom loop starts at i = 1 The center row prints twice. Start the bottom half at i = 2.
no bottom Forget the second outer loop You get Program 46’s square only. Add for (i = 2; i <= k; i++).
j = 1 Right half starts at j = 1 The center digit doubles on every row. Keep j = 2..k.
k = 1 Single cell Output is just 1 — the bottom loop never runs.
WriteLine WriteLine inside a half-loop That breaks the row. Call WriteLine only after both inner loops.
Bad input Convert.ToInt32 throwsPrefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity Program Time Extra space Fixed / compact (Examples 1, 3) O(k²)O(1)User input (Example 2) O(k²)O(1)
The grid is (2k - 1) × (2k - 1), so total prints are about 4k² — quadratic in k.
Remember
Key Takeaways Rule: top k..1, bottom 2..k; cell = j > i ? j : i.
Shape: Program 46’s square plus a mirror — size (2k - 1)².
Write vs WriteLine: numbers stay on the line; WriteLine advances after both halves.
Next step: Program 48 prints the powers-of-11 sequence (1, 11, 121, …).
One line: print concentric rows inward to 1, then mirror them outward to finish the diamond.
Frequently Asked Questions What does this pattern print? A full concentric number diamond: outer layer k, values decrease to 1 at the center, then increase back to k — a (2k-1)×(2k-1) grid.
Why are there two outer loops? The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) to mirror the shape.
How is this different from Program 46? Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
What does j > i mean? When column j is still outside the current row layer i, print j. Otherwise print i (the current layer value).
How wide is the output? Each row has 2*k - 1 numbers. With k = 5, width and height are both 9.
How can I change k? Change k or read it from input — both outer loops and row width adjust automatically — see Example 2.
What is the time complexity? O(k²) because the grid has about (2k-1)² cells and each is printed once.
What does the center cell print? The center prints 1 — the deepest layer of the concentric pattern.
Why start the bottom at i = 2? Row i = 1 is already printed by the top half. Starting at 2 avoids duplicating the center row.
🤔
Did you know? Top half: for (i = k; i >= 1; i--). Bottom half: for (i = 2; i <= k; i++). Each cell: j > i ? j : i. Grid size = 2k - 1 rows and columns.
Next: Powers of 11 Pattern Print 1, 11, 121, 1331, 14641 with a running multiply-by-11 state.
Program 48 tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful
Helpful Share Copy link Suggestion