Reverse Alphabet, Diagonal * in C#

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

What You’ll Learn

Print the reverse alphabet line EDCBA on every row, but replace one character per row with * on the diagonal where i == j.

The star slides from right to left: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.

⭐ Pattern Output

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Complete C# Program

Outer i runs A..E. Inner j runs E..A. When i == j, print *; otherwise print j.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (char i = 'A'; i <= 'E'; i++)
            {
                for (char j = 'E'; j >= 'A'; j--)
                {
                    if (i == j)
                        Console.Write("*");
                    else
                        Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Outer loop chooses the diagonal letter

i takes A, B, C, D, E across rows.

Rows
2

Inner loop prints E..A

j runs from E down to A, so the default row is EDCBA.

Reverse
3

Replace diagonal with *

When i == j, print *. That happens at a different column each row, shifting the star.

Diagonal
=

One star per row

This is a 5×5 grid pattern, so the total work is O(n²) for n letters.

2

Variation — User Input Version

Read the top letter (like E) and generate the same pattern for A..top. (Cap at Z if desired.)

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the top letter (like E): ");
            char top = Convert.ToChar(Console.ReadLine());

            for (char i = 'A'; i <= top; i++)
            {
                for (char j = top; j >= 'A'; j--)
                {
                    Console.Write(i == j ? '*' : j);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Replace * with another symbol like #
  • Print ABCDE instead by making the inner loop go forward
  • Compare with Program 16 (centering using spaces)
  • Use lowercase by switching to 'a'..

Avoid

  • Using different bounds for i and j (diagonal won’t line up)
  • Forgetting that inner loop direction controls whether output is E..A or A..E
  • Letting ranges exceed the alphabet without planning (beyond Z)

Key Takeaways

1

Inner loop prints reverse letters (E..A).

2

i == j marks one diagonal cell per row.

3

Star shifts one column each row.

4

Complexity is O(n²).

❓ Frequently Asked Questions

The star prints when i == j, so each row replaces exactly one character at the matching column.
Yes. Replace "*" with any symbol (like "#" or "@") in the conditional branch.
O(n²) for n letters because you scan a full n-by-n grid.

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