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
Alphabet Hourglass Pattern output
Output (n = 4):
ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG

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.
- Run the outer loop for
ifromndown to1, one row per pass (upper half). - Print
n β ispaces. - Print
2 Γ i β 1letters (letter numberj). - Move to the next line.
- Run the outer loop for
ifrom2ton, one row per pass (lower half). - Print
n β ispaces. - Print
2 Γ i β 1letters (letter numberj). - 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 β 1letters. - 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.






