Diamond Alphabet & Stars in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
A
1

Complete C# Program (Half Height 5)

Odd j prints the row letter; even j prints *. Upper half prints 1..5, lower half prints 4..1.

C#
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

1

Upper half: 1 to n

The first outer loop runs i = 1..5, making the row length grow.

Up
2

Odd/even positions alternate

Inner loop prints 2i-1 characters: odd j prints the letter, even j prints *.

Alternate
3

Lower half mirrors down

Second outer loop runs i = 4..1 so the widest row (E*E*E*E*E) is not duplicated.

Mirror
=

Diamond made from rows

Total printed characters scale like O(n²) for half height n.

2

Variation — User Input Half Height

Let the user choose the half height (n). Uses a computed row letter instead of an array.

C#
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-1 characters)
  • Letting n exceed 26 without handling letters beyond Z

Key Takeaways

1

Upper loop grows from 1 to n; lower loop mirrors back.

2

Each row prints 2i-1 characters.

3

j % 2 alternates letter and *.

4

Start the lower half at n-1 to avoid duplication.

❓ Frequently Asked Questions

The inner loop checks the position: even columns print *, odd columns print the row letter. That alternates symbols cleanly.
The widest row is already printed in the upper half. Starting from n-1 mirrors the shape without duplicating the middle row.
O(n²) because there are about 2n-1 rows and each row prints O(n) characters.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful