New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Alphabet Pyramid Pattern

The alphabet pyramid prints A on the top row, A B on the second and so on, with each row centred. It is the number pyramid with letters in place of digits.

Quick answer: Row i prints n − i spaces and then the first i letters, each followed by a space. The letter at position j is 'A' + j − 1: adding a number to a character moves that many places along the alphabet.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet pyramid pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Alphabet Pyramid Pattern output

Output (n = 5):

    A
   A B
  A B C
 A B C D
A B C D E
Alphabet Pyramid Pattern layout diagram, row by row (n = 5)
How each row of the alphabet pyramid pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Centred Repeated Number Pyramid, Continuous Alphabet Pyramid, Dollar ($) Pyramid, Number Pyramid Pattern and Pascal’s Triangle programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the alphabet pyramid pattern works

Characters are stored as numbers, so 'A' + 2 is 'C'. That makes letter patterns the same as number patterns with an offset: wherever a number pattern prints j, a letter pattern prints 'A' + j − 1. The spacing works as in any centred pyramid: each letter and its space take two characters, so one less leading space per row keeps the rows centred.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n − i spaces.
  3. Print i letters (letter number j).
  4. Move to the next line.

Key takeaways

  • A letter is a number: 'A' + k is the letter k places after A.
  • Any number pattern becomes a letter pattern by printing 'A' + value − 1.
  • In Java and C++ cast the result back to char; in Python use chr(65 + k).
  • With 26 letters, n can go up to 26 before you run out of alphabet.

Alphabet Pyramid Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= i; j++)
            printf("%c ", 'A' + j - 1);
        printf("\n");
    }
    return 0;
}

Alphabet Pyramid Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= i; j++)
            cout << char('A' + j - 1) << " ";
        cout << endl;
    }
    return 0;
}

Alphabet Pyramid Pattern in Java

public class AlphabetPyramid {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print((char) ('A' + j - 1) + " ");
            System.out.println();
        }
    }
}

Alphabet Pyramid Pattern in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, i + 1):
        print(chr(ord('A') + j - 1), end=" ")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

Why does Java need a cast to char?

In Java, 'A' + 2 is an int (67). Printing it shows 67, not C. Casting with (char) turns it back into the letter.

How do I print lowercase letters?

Start from 'a' instead of 'A'. Everything else stays the same.

What happens if n is larger than 26?

The letters run past Z into other characters: 'A' + 26 is the bracket [. Wrap around with (j − 1) % 26 if you need more than 26 positions.

How do I make every row repeat its own letter (A, B B, C C C)?

Print 'A' + i − 1 instead of 'A' + j − 1. The letter then depends on the row, not the position.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 13 of 19 in Pyramid Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.