Concentric Number Diamond in C#

What You’ll Learn
How to print a concentric number diamond where values decrease toward the center and then increase back out.
This is built by printing the top half (k down to 1) and then mirroring it for the bottom half (2 up to k).
⭐ Pattern Output
For k = 5, the pattern looks like this:
5 5 5 5 5 5 5 5 5\n5 4 4 4 4 4 4 4 5\n5 4 3 3 3 3 3 4 5\n5 4 3 2 2 2 3 4 5\n5 4 3 2 1 2 3 4 5\n5 4 3 2 2 2 3 4 5\n5 4 3 3 3 3 3 4 5\n5 4 4 4 4 4 4 4 5\n5 5 5 5 5 5 5 5 5Complete C# Program
Use two passes: the first prints rows from k down to 1, and the second prints rows from 2 up to k.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
int k = 5;
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
Pick the outer value \(k\)
int k = 5; decides the outermost number and the grid size.
Print the top half
for (i = k; i >= 1; i--) prints rows from k down to 1, shrinking toward the center.
Build a row (left + right)
Each row is printed in two parts: the left side counts k..1, and the right side mirrors with 2..k.
Choose the cell value
The rule (j > i ? j : i) prints the larger value so the border stays at k and values decrease toward the center.
Mirror the bottom half
for (i = 2; i <= k; i++) prints rows back from 2 up to k to complete the diamond.
Concentric number diamond
The output size is \(2k - 1\) by \(2k - 1\), so the time complexity is O(k²).
Variation — User Input k
Let the user choose the outer value \(k\).
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();
}
}
}
}💡 Tips for Enhancement
Try These
- Print without spaces to make it a tight 9x9 grid
- Change the filler (space after numbers) to a tab for wider separation
- Set
kto 7 or 9 to generate a larger concentric diamond - Replace numbers with characters to create alphabet concentric patterns
Avoid
- Using a non-positive
k(validate input when taking user values) - Forgetting the bottom half loop (the pattern won’t be symmetric)
- Using inconsistent spacing per cell (alignment will break)
Key Takeaways
Width (and height) of the grid is \(2k - 1\).
Top half prints rows k..1, bottom half prints 2..k.
Each cell uses the same rule: print the larger of the row-layer and column-layer values.
Time complexity is O(k²) because the grid area is about \((2k-1)^2\).
❓ Frequently Asked Questions
k is the outer layer value. All border cells belong to the outermost layer, so they print k (5 in this example).1 because it’s the deepest layer of the concentric pattern.k = 7. The size becomes 2k-1 = 13.Console.Write(value + " "); with Console.Write(value);. For double-digit values, consider fixed-width formatting.Explore More C# Number Patterns!
Concentric patterns are excellent practice for symmetry, layers, and loop boundaries.
Most concentric square/diamond patterns can be generated by computing a cell’s distance from the border and mapping that distance to a value.
12 people found this page helpful
