Print an Alphabet Hourglass Pattern in C

To print an alphabet hourglass pattern in C, we use nested loops to display characters in a symmetrical shape that starts with the widest row at the top and gradually narrows down, then expands back symmetrically. The pattern consists of uppercase or lowercase letters arranged in an hourglass formation.


Examples of Alphabet Hourglass Patterns

1. Alphabet Hourglass Pattern Using Uppercase Letters

In this example, we will print an alphabet hourglass pattern using uppercase letters. The largest row at the top starts with ‘A’ through the chosen letter (e.g., ‘E’), then narrows down to a single letter and expands back.

main.c

</>
Copy
#include <stdio.h>

void printHourglass(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) 
            printf("  "); // Printing spaces
        
        for (char ch = 'A'; ch <= 'A' + (n - i - 1); ch++) 
            printf("%c ", ch); // Printing left side characters
        
        for (char ch = 'A' + (n - i - 2); ch >= 'A'; ch--) 
            printf("%c ", ch); // Printing right side characters
        
        printf("\n");
    }

    for (int i = n - 2; i >= 0; i--) {
        for (int j = 0; j < i; j++) 
            printf("  "); 
        
        for (char ch = 'A'; ch <= 'A' + (n - i - 1); ch++) 
            printf("%c ", ch);
        
        for (char ch = 'A' + (n - i - 2); ch >= 'A'; ch--) 
            printf("%c ", ch);
        
        printf("\n");
    }
}

int main() {
    int n = 5; // Defines the number of rows
    printHourglass(n);
    return 0;
}

Explanation:

  1. We define a function printHourglass(int n) where n is the number of rows.
  2. We use two nested loops to print the upper half of the hourglass.
  3. The first loop prints spaces to align the characters properly.
  4. The next two loops print characters in a pyramid-like fashion, forming a mirrored pattern.
  5. The second set of loops prints the lower half of the hourglass by reversing the logic of the first half.
  6. The main() function calls printHourglass() with n = 5.

Output:

A B C D E D C B A 
  A B C D C B A 
    A B C B A 
      A B A 
        A 
      A B A 
    A B C B A 
  A B C D C B A 
A B C D E D C B A

2. Alphabet Hourglass Pattern Using Lowercase Letters

In this example, we modify the previous program to use lowercase letters instead of uppercase letters.

main.c

</>
Copy
#include <stdio.h>

void printHourglassLowercase(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) 
            printf("  ");
        
        for (char ch = 'a'; ch <= 'a' + (n - i - 1); ch++) 
            printf("%c ", ch);
        
        for (char ch = 'a' + (n - i - 2); ch >= 'a'; ch--) 
            printf("%c ", ch);
        
        printf("\n");
    }

    for (int i = n - 2; i >= 0; i--) {
        for (int j = 0; j < i; j++) 
            printf("  ");
        
        for (char ch = 'a'; ch <= 'a' + (n - i - 1); ch++) 
            printf("%c ", ch);
        
        for (char ch = 'a' + (n - i - 2); ch >= 'a'; ch--) 
            printf("%c ", ch);
        
        printf("\n");
    }
}

int main() {
    int n = 5;
    printHourglassLowercase(n);
    return 0;
}

Explanation:

  1. We create a function printHourglassLowercase(int n) that follows the same logic as the uppercase version but starts from 'a'.
  2. The loops work exactly like the previous example but print lowercase letters.
  3. The main() function calls printHourglassLowercase() with n = 5.

Output:

a b c d e d c b a 
  a b c d c b a 
    a b c b a 
      a b a 
        a 
      a b a 
    a b c b a 
  a b c d c b a 
a b c d e d c b a

Conclusion

In this tutorial, we explored how to print an alphabet hourglass pattern in C:

  1. We printed an hourglass pattern using uppercase letters.
  2. We modified the program to use lowercase letters.
  3. The pattern is achieved using nested loops, where spaces and character alignment are carefully controlled.