Diamond Alphabet & Stars in C#

What You’ll Learn
Build a vertical diamond where each row repeats the same letter, with * between letters. The pattern widens to the middle row, then mirrors back down.
If you like mirrored effects, also see Program 19 (letters with spaces) and Program 15 (stars in the center).
⭐ Pattern Output
Half height 5:
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
AComplete C# Program (Half Height 5)
Odd j prints the row letter; even j prints *. Upper half prints 1..5, lower half prints 4..1.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
Console.Write("*");
else
Console.Write(alpha[i - 1]);
}
Console.WriteLine();
}
for (int i = 4; i >= 1; i--)
{
for (int j = 1; j < i * 2; j++)
{
if (j % 2 == 0)
Console.Write("*");
else
Console.Write(alpha[i - 1]);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Upper half: 1 to n
The first outer loop runs i = 1..5, making the row length grow.
Odd/even positions alternate
Inner loop prints 2i-1 characters: odd j prints the letter, even j prints *.
Lower half mirrors down
Second outer loop runs i = 4..1 so the widest row (E*E*E*E*E) is not duplicated.
Diamond made from rows
Total printed characters scale like O(n²) for half height n.
Variation — User Input Half Height
Let the user choose the half height (n). Uses a computed row letter instead of an array.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter half height (like 5): ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
char ch = (char)('A' + i - 1);
for (int j = 1; j < i * 2; j++)
Console.Write(j % 2 == 0 ? "*" : ch.ToString());
Console.WriteLine();
}
for (int i = n - 1; i >= 1; i--)
{
char ch = (char)('A' + i - 1);
for (int j = 1; j < i * 2; j++)
Console.Write(j % 2 == 0 ? "*" : ch.ToString());
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Swap
*for another separator like- - Print spaces around
*for a wider shape - Use lowercase by changing the base to
'a' - Combine with centering spaces to make a true diamond in 2D
Avoid
- Starting the lower half at
n(duplicates the widest row) - Using an even row length (you need
2i-1characters) - Letting
nexceed 26 without handling letters beyond Z
Key Takeaways
Upper loop grows from 1 to n; lower loop mirrors back.
Each row prints 2i-1 characters.
j % 2 alternates letter and *.
Start the lower half at n-1 to avoid duplication.
❓ Frequently Asked Questions
*, odd columns print the row letter. That alternates symbols cleanly.n-1 mirrors the shape without duplicating the middle row.2n-1 rows and each row prints O(n) characters.12 people found this page helpful
