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

Alphabet Hourglass Pattern

The alphabet hourglass, also called the alphabet sandglass, starts with ABCDEFG, narrows to a single A in the middle and widens again. It is the alphabet diamond with its halves swapped.

Quick answer: Use the same row as in the alphabet diamond: n βˆ’ i spaces and the first 2i βˆ’ 1 letters. Print the rows from i = n down to 1, then from 2 back up to n. Starting the second loop at 2 keeps the single A from appearing twice.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet hourglass 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 Hourglass Pattern output

Output (n = 4):

ABCDEFG
 ABCDE
  ABC
   A
  ABC
 ABCDE
ABCDEFG
Alphabet Hourglass Pattern layout diagram, row by row (n = 4)
How each row of the alphabet hourglass pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the alphabet hourglass pattern works

An hourglass and a diamond use exactly the same rows; they only print them in a different order. The diamond grows and then shrinks, the hourglass shrinks and then grows. Because each row is worked out from i alone, changing the order of the loops is all it takes.

  1. Run the outer loop for i from n down to 1, one row per pass (upper half).
  2. Print n βˆ’ i spaces.
  3. Print 2 Γ— i βˆ’ 1 letters (letter number j).
  4. Move to the next line.
  5. Run the outer loop for i from 2 to n, one row per pass (lower half).
  6. Print n βˆ’ i spaces.
  7. Print 2 Γ— i βˆ’ 1 letters (letter number j).
  8. Move to the next line.

Key takeaways

  • The hourglass uses the same rows as the diamond, in a different order.
  • Every row starts at A and holds 2i βˆ’ 1 letters.
  • The second loop starts at 2 so the middle A is printed once.
  • It matches the hourglass star pattern with letters.

Alphabet Hourglass Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    // upper half
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("%c", 'A' + j - 1);
        printf("\n");
    }

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

Alphabet Hourglass Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    // upper half
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << char('A' + j - 1);
        cout << endl;
    }

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

Alphabet Hourglass Pattern in Java

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

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

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

Alphabet Hourglass Pattern in Python

n = 4

# upper half
for i in range(n, 0, -1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i):
        print(chr(ord('A') + j - 1), end="")
    print()

# lower half
for i in range(2, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i):
        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

How do I make the middle row show two A rows?

Start the second loop at 1 instead of 2. The A is then printed twice, which some books show as the “neck” of the glass.

Can every row count backwards (GFEDCBA)?

Yes. Print 'A' + 2i βˆ’ 1 βˆ’ j instead: each row then starts at its last letter and counts back to A.

How do I let the user choose the size?

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