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

Alphabet Palindrome Pyramid (A, ABA, ABCBA)

The alphabet palindrome pyramid prints rows that read the same forwards and backwards: A, ABA, ABCBA. Each row climbs to its middle letter and comes back down.

Quick answer: Each row has three parts: n βˆ’ i spaces, the letters A up to the i-th letter, then the letters back down to A without repeating the middle one. The falling part prints 'A' + i βˆ’ j βˆ’ 1 for j from 1 to i βˆ’ 1.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Alphabet Palindrome Pyramid output
  2. How the alphabet palindrome pyramid works
  3. Alphabet Palindrome Pyramid in C
  4. Alphabet Palindrome Pyramid in C++
  5. Alphabet Palindrome Pyramid in Java
  6. Alphabet Palindrome Pyramid in Python
  7. Time and space complexity
  8. Frequently asked questions

Alphabet Palindrome Pyramid output

Output (n = 5):

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
Alphabet Palindrome Pyramid layout diagram, row by row (n = 5)
How each row of the alphabet palindrome pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Continuous Number Pyramid, Full Pyramid Star Pattern and Palindrome Number Pyramid 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 palindrome pyramid works

This is the palindrome number pyramid (1, 121, 12321) with letters. The rising half prints the letters 1 to i; the falling half prints i βˆ’ 1 down to 1. Leaving the middle letter out of the falling half is what keeps each row an odd length and stops the peak from doubling.

  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. Print i βˆ’ 1 letters (letter number i βˆ’ j).
  5. Move to the next line.

Key takeaways

  • Every row is a palindrome with its peak in the middle.
  • The rising half has i letters, the falling half i βˆ’ 1.
  • The rows have no spaces between letters, so n βˆ’ i leading spaces centre them.
  • Print the falling rows again in reverse to make an alphabet diamond.

Alphabet Palindrome Pyramid 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);
        for (int j = 1; j <= i - 1; j++)
            printf("%c", 'A' + i - j - 1);
        printf("\n");
    }
    return 0;
}

Alphabet Palindrome Pyramid 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);
        for (int j = 1; j <= i - 1; j++)
            cout << char('A' + i - j - 1);
        cout << endl;
    }
    return 0;
}

Alphabet Palindrome Pyramid in Java

public class AlphabetPalindromePyramid {
    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));
            for (int j = 1; j <= i - 1; j++)
                System.out.print((char) ('A' + i - j - 1));
            System.out.println();
        }
    }
}

Alphabet Palindrome Pyramid 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="")
    for j in range(1, i):
        print(chr(ord('A') + i - 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 the middle letter appear only once?

The falling half starts one letter below the peak. If it started at the peak, every row would have a doubled middle letter, such as ABCCBA.

How do I turn it into a diamond?

After the pyramid, print the rows again from n βˆ’ 1 down to 1 with the same formulas.

How do I let the user choose the number of rows?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

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 14 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.