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

Hollow Alphabet Pyramid

The hollow alphabet pyramid draws only the outline of a pyramid, writing each row’s letter on its two edges: A at the top, then B B, C C, D D, and a full base of E.

Quick answer: Treat row i as n + i βˆ’ 1 positions. Print the row letter 'A' + i βˆ’ 1 at the two edges, positions n βˆ’ i + 1 and n + i βˆ’ 1, and on every position of the last row. Everything else is a space.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow alphabet pyramid 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

Hollow Alphabet Pyramid output

Output (n = 5):

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

Same shape, other characters: the Hollow Number Pyramid and Hollow Pyramid Star Pattern 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 hollow alphabet pyramid works

The outline is the same as in the hollow pyramid star pattern. Row i of a pyramid runs from column n βˆ’ i + 1 to n + i βˆ’ 1, and only those two ends are printed, except on the base, which closes the shape. Using the row letter instead of a star makes it easy to see which row each part of the outline belongs to.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n + i βˆ’ 1 positions in the row. Print letters (letter number i) where j > n βˆ’ i and (i = n or j = n βˆ’ i + 1 or j = n + i βˆ’ 1); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Only the two edges and the base are printed.
  • The letter depends on the row: A on row 1, E on row 5.
  • j > n βˆ’ i keeps the leading spaces out of the test.
  • It is the letter version of the hollow number pyramid.

Hollow Alphabet 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 - 1; j++) {
            if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
                printf("%c", 'A' + i - 1);
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Alphabet 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 - 1; j++) {
            if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
                cout << char('A' + i - 1);
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Alphabet Pyramid in Java

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

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

Hollow Alphabet Pyramid in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n + i):
        if j > n - i and (i == n or j == n - i + 1 or j == n + i - 1):
            print(chr(ord('A') + i - 1), end="")
        else:
            print(" ", 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 do I spell out the base as ABCDEFGHI?

On the last row print 'A' + j βˆ’ 1 instead of the row letter. The edges keep their row letters.

Why is the second row not hollow?

It has only three positions and the outer two are edges, so the gap is a single space. The gap grows by two each row after that.

Can I use lowercase letters?

Yes. Start from 'a' instead of 'A'.

How do I make it a hollow alphabet diamond?

Print this pattern, then the same rows from n βˆ’ 1 back to 1 without filling the last row. The edges then close in a point at the bottom.

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