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

Mirrored Alphabet Pyramid (A, B A B, C B A B C)

The mirrored alphabet pyramid puts A in the middle of every row and builds outwards: A, then B A B, then C B A B C. It is the alphabet palindrome pyramid turned inside out.

Quick answer: Row i prints n − i double spaces, then the letters from i down to A ('A' + i − j), then from B back up to i ('A' + j), each letter followed by a space.

This program is part of the String Patterns collection.

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

Mirrored Alphabet Pyramid output

Output (n = 4):

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

How the mirrored alphabet pyramid works

In the palindrome pyramid the highest letter sits in the middle. Here it is the other way round: A is in the centre and the letters rise towards both ends. The left half counts down to A and the right half counts back up without repeating A. Each letter takes two characters with its space, so the indent is two spaces per missing letter.

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

Key takeaways

  • A sits in the centre of every row.
  • The left half counts down to A; the right half counts back up from B.
  • Each letter with its space is two characters, so the indent uses double spaces.
  • The edges read A, B, C, D from top to bottom.

Mirrored Alphabet Pyramid in C

#include <stdio.h>

int main(void) {
    int n = 4;

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

Mirrored Alphabet Pyramid in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

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

Mirrored Alphabet Pyramid in Java

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

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

Mirrored Alphabet Pyramid in Python

n = 4

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') + i - j + 1 - 1), end=" ")
    for j in range(1, i):
        print(chr(ord('A') + j + 1 - 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

How is it different from A, ABA, ABCBA?

In the alphabet palindrome pyramid the highest letter is in the middle. Here A is in the middle and the highest letters are at the edges.

Can I print it with numbers?

Yes: print i − j + 1 and j + 1 instead of letters. Row 3 becomes 3 2 1 2 3.

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 20 of 22 in String 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.