C# Alphabet Pyramid Pattern (Centered)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A centered alphabet pyramid prints odd-width rows (1, 3, 5, …) with leading spaces for centering, while letters advance continuously across the whole triangle — A, then B C D, then E F G H I.

Remember
Rule: pad spaces + next odd count of letters (running k)

    A
  B C D
E F G H I     ← 3 rows (bottom width 5)

Unlike Program 14 (each row restarts at A), here one counter walks the alphabet across every row. Leading spaces keep each line under the widest bottom row.

How to Solve It

Step odd row widths, pad with spaces, then print the next letters from a running counter.

MethodIdeaBest for
Scan columnsInner loop walks bottom width; print space or next letterLearning one-loop centering
Explicit padSeparate pad loop, then letter loopClearer reading; row-count input

Pseudocode

Pseudocode
k = 'A'
width = 2 * rows - 1
for i from 1 to width step 2:
    for j from width down to 1:
        if j > i:
            print space
        else:
            print k and a space
            k = next letter
    print newline

Cheat sheet

GoalPattern
Odd row widthsfor (int i = 1; i <= width; i += 2)
Scan bottom widthfor (int j = width; j >= 1; j--)
Pad or letterif (j > i) Console.Write(" "); else Console.Write(k + " ");
Advance letterk++; (or index into an alphabet array)
Explicit padpad = width - letters; then print pad spaces
End the rowConsole.WriteLine();
A–Y-safe heightMax 5 rows (1+3+…+9 = 25 letters)

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach space and each letter (often with a trailing space)
Console.WriteLineEnds the current lineAfter the column scan / letter loop

Live Preview

Change the row count and the centered pyramid updates instantly — including bottom width and letter totals.

Whole numbers from 1 to 5. Bottom width is 2 × rows - 1; total letters equal rows² (stays within A–Y).

Live result 3 rows · width 5 · 9 letters
    A
  B C D
E F G H I

Worked Walkthrough — rows = 3 (width 5)

Trace each odd width, how many pads print, and which letters the running counter emits.

RowWidth iPadsLettersPrinted row
114AA
232B C DB C D
350E F G H IE F G H I

Total letters: 1 + 3 + 5 = 9 = 3². The counter never resets — it keeps climbing past each row.

C# Programs

Three complete programs: fixed width 5, odd-width input, and an explicit pad-then-letters form. Use View Output to reveal sample results.

Example 1 — Fixed bottom width 5

Hard-coded bounds — ideal for first demos and screenshots.

C#
using System;

class Program
{
    static void Main()
    {
        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. Outer loop steps odd widths. i takes 1, 3, 5 — the letter count for each row.

2. Inner loop scans the bottom width. While j > i, print a pad space; otherwise print the next alphabet letter plus a separator space.

3. Running index. k never resets, so letters flow A → B C D → E F G H I across the pyramid.

Example 2 — Odd Width Input

Read the bottom width as an odd number (like 5 or 7). Prefer int.TryParse and odd validation in real apps.

C#
using System;

class Program
{
    static void Main()
    {
        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();
        }
    }
}

How It Works

1. Same centering scan. Only the outer/inner bounds follow width instead of the literal 5.

2. Char counter. k starts at 'A' and increments after each printed letter.

3. Safer input tip. Require a positive odd width and stay within A–Z:

Safer input
if (!int.TryParse(Console.ReadLine(), out int width)
    || width < 1 || width % 2 == 0 || width > 9)
{
    Console.WriteLine("Enter an odd width from 1 to 9.");
    return;
}

Example 3 — Pad Spaces, Then Letters

Often clearer to read: print leading spaces first, then the odd letter count.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 3;
        int width = 2 * rows - 1;
        char k = 'A';

        for (int row = 1; row <= rows; row++)
        {
            int letters = 2 * row - 1;
            int pad = width - letters;

            for (int s = 0; s < pad; s++)
            {
                Console.Write(" ");
            }

            for (int L = 0; L < letters; L++)
            {
                Console.Write(k);
                if (L < letters - 1) Console.Write(" ");
                k++;
            }

            Console.WriteLine();
        }
    }
}

How It Works

1. Map row → letters. Row r needs 2r - 1 letters and width - letters leading spaces.

2. Two clear loops. First pad, then print letters with separators between them — same visual pyramid as the scan version.

3. Row-count friendly. Driving from rows makes width = 2 * rows - 1 automatic.

Edge Cases & Pitfalls

Check these before calling the solution done.

Even width

Broken pyramid

An even bottom width never lands on a clean 1, 3, 5, … stack. Require an odd positive width.

Reset k

Wrong letters

Do not reset the letter counter each row. Resetting turns this into a different pattern (fresh A on every line).

No pads

Left-aligned triangle

Skipping the space branch left-aligns the odd rows — you lose the centered pyramid look.

WriteLine early

Broken row

Call WriteLine only after the column scan. Inside the inner loop, each cell lands on its own line.

rows = 1

Single A

Output is just A — a good sanity check.

Past Z

Clamp to 5 rows

Five rows use 25 letters (A–Y). Six rows need 36 — past Z. Cap height or wrap with care.

Time and Space Complexity

ProgramTimeExtra space
Scan / explicit pad formsO(r²)O(1)

Each of r rows scans about 2r columns (or prints O(r) pads and letters). Total letters printed equal r².

Key Takeaways

  • Odd widths: outer loop steps 1, 3, 5, … under a matching bottom width.
  • Center with pads: print spaces while the column is outside the current row width.
  • Running counter: one k walks the alphabet across every row — do not reset it.
  • Complexity: O(r²) time because total letters equal r²; O(1) extra space.

One line: for each odd width, pad spaces then print the next letters from a continuous counter.

Frequently Asked Questions

The inner loop scans a fixed bottom width. While the column index is still outside the current row width, it prints spaces; otherwise it prints the next letter.
The outer loop increases by 2 (1, 3, 5), so each row prints an odd number of letters, forming a pyramid shape.
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).
Console.Write stays on the same line. Console.WriteLine ends the current line. Spaces and letters use Write; the row break uses WriteLine after the inner scan.
O(r²) for r rows because each row scans a fixed-width set of columns and prints a growing number of characters overall.
Prefer int.TryParse and require an odd positive width (1, 3, 5, …). Cap so total letters 1+3+…+width stay within A–Z if you want only alphabetic output.

Did you know?

This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids). Letters flow continuously via one counter — A, then B C D, then E F G H I — while leading spaces keep each row aligned under the widest line.

Next: Reverse Diagonal Star

Descending letters with a star marking the diagonal.

Program 17 tutorial →

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