Centered Alphabet Pyramid in C#

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

What You’ll Learn

Print a small centered pyramid: one letter on the first row, then 3 letters, then 5 letters, with leading spaces so it looks aligned.

Letters flow continuously via one counter (k): A, then B C D, then E F G H I.

⭐ Pattern Output

Three rows (spaces between letters; leading spaces align columns):

Output
    A
  B C D
E F G H I
1

Complete C# Program

Outer loop prints 1, 3, 5 letters. Inner loop scans 5 columns; it prints spaces first, then letters (with a trailing space) to keep the pyramid aligned.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int k = 0;
            char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            for (int i = 1; i <= 5; i += 2)
            {
                for (int j = 5; j >= 1; j--)
                {
                    if (j > i)
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write(alpha[k++] + " ");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

One running counter

k starts at 0 and increments after every printed letter, so output stays sequential.

A, B, C, ...
2

Odd row widths

Outer i runs 1, 3, 5. That determines how many letters appear on each row.

1, 3, 5
3

Leading spaces for centering

The inner loop checks j > i. Those positions print spaces so shorter rows are padded on the left.

Padding
=

Complexity

For r rows, you scan O(r) positions per row, so overall complexity is O(r²).

2

Variation — User Input (Odd Rows)

Read the bottom width as an odd number (like 7). The outer loop prints 1..width stepping by 2, and the inner loop scans a fixed width each row.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the bottom width (odd number): ");
            int width = Convert.ToInt32(Console.ReadLine());

            char k = 'A';
            for (int i = 1; i <= width; i += 2)
            {
                for (int j = width; j >= 1; j--)
                {
                    if (j > i)
                    {
                        Console.Write(" ");
                    }
                    else
                    {
                        Console.Write(k + " ");
                        k++;
                    }
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Print without trailing spaces by controlling separators (like in program 13)
  • Use lowercase by starting with k = 'a'
  • Cap output at 'Z' if you only want A–Z
  • Compare with Program 14 (odd-width without centering)

Avoid

  • Even widths if you want symmetric odd rows (use odd widths like 5, 7, 9)
  • Resetting the letter counter each row if you want continuous letters
  • Forgetting padding spaces (rows will shift left)

Key Takeaways

1

Outer loop controls odd row widths (1, 3, 5...).

2

Inner loop prints spaces first to center the row.

3

One running counter prints consecutive letters.

4

Complexity is O(r²) for r rows.

❓ Frequently Asked Questions

Increase the maximum odd width (and loop bounds). The number of rows grows as width grows: 1, 3, 5, 7...
They shift each row to the right so the odd-width lines are centered under the widest line.
The trailing space makes columns easier to see. If you want a compact output, print just k (or control separators carefully).
O(n²) in the number of rows, because each row prints a proportional amount of characters.

Explore More C# Alphabet Patterns!

Once you can center output with spaces, you can build diamond and pyramid variations too.

All Alphabet Patterns →
Did you know?

This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids).

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