In this tutorial, we will write a C program to print half hourglass pattern of characters. Before that, you may go through the following topic in C.
We will go through the two different half hourglass of alphabets with two different character patterns.
Half Hourglass Alphabet Pattern in C
Pattern 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> void main() { int rows, i, j; printf("Enter the no. of rows: "); scanf("%d", &rows); //upper part for (i = rows; i >= 0; i--) { int character = 65; for (j = 0; j <= i; j++) printf("%c ", (char)(character + j)); printf("\n"); } //lower part for (i = 0; i <= rows; i++) { int character = 65; for (j = 0; j <= i; j++) printf("%c ", (char)(character + j)); printf("\n"); } getch(); } |
Output:
Enter the no. of rows: 5
A B C D E F
A B C D E
A B C D
A B C
A B
A
A
A B
A B C
A B C D
A B C D E
A B C D E F
Pattern 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <stdio.h> void main() { int rows, i, j; printf("Enter the no. of rows: "); scanf("%d", &rows); //upper part for (i = rows; i >= 0; i--) { int character = 65; for (j = i; j >= 0; j--) printf("%c ", (char)(character + j)); printf("\n"); } //lower part for (i = 0; i <= rows; i++) { int character = 65; for (j = i; j >= 0; j--) printf("%c ", (char)(character + j)); printf("\n"); } getch(); } |
Output:
Enter the no. of rows: 5
F E D C B A
E D C B A
D C B A
C B A
B A
A
A
B A
C B A
D C B A
E D C B A
F E D C B A