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

Hollow Alphabet Triangle

The hollow alphabet triangle keeps only the edges of the alphabet triangle: the first column of A, the slanted side and the full bottom row ABCDE. Each letter stays in its own column.

Quick answer: Use the right-triangle loops: row i has i positions. Print the letter 'A' + j − 1 when j == 1, j == i or i == n, and a space for every other position.

This program is part of the String Patterns collection.

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

Output (n = 5):

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

Same shape, other characters: the Hollow Number Triangle and Hollow Right Triangle 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 triangle works

The full triangle would print A, AB, ABC and so on. The hollow version uses the same positions but only fills the edges. Taking the letter from the column number means that a letter is always the same in a given column, so the slanted edge reads A, B, C, D, E from top to bottom.

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

Key takeaways

  • The edges are j == 1, j == i and the last row.
  • The letter follows the column, so every column has one letter.
  • The gap inside grows by one space each row.
  • It matches the hollow number triangle with letters.

Hollow Alphabet Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Hollow Alphabet Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

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

Hollow Alphabet Triangle in Java

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

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

Hollow Alphabet Triangle in Python

n = 5

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

Why is row 3 “A C” and not “A B”?

The C is in column 3. Since the letter comes from the column, position 3 is always C. To print “A B” instead, use a counter that only goes up when a letter is printed.

How do I fill the edges with the row letter?

Print 'A' + i − 1 instead. The edges then show A, BB, C C, D D, EEEEE.

How do I make a hollow alphabet pyramid?

Centre the rows and test the two ends of each row, as in the hollow alphabet pyramid.

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