In this tutorial, we will write a C Program to display half hourglass patterns using numbers. Before that, you may go through the following topic in C.
Half Hourglass number pattern in C
1. Left half hourglass number pattern
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 32 33 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j < i; j++) printf(" "); for (j = i; j <= rows; j++) printf("%d", j); printf("\n"); } for (i = rows - 1; i >= 1; i--) { for (j = 1; j < i; j++) printf(" "); for (j = i; j <= rows; j++) printf("%d", j); printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | Enter the number of rows: 5 12345 2345 345 45 5 45 345 2345 12345 |
2. Right half hourglass number pattern
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 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) printf("%d", j); printf("\n"); } for (i = 2; i <= rows; i++) { for (j = 1; j <= i; j++) printf("%d", j); printf("\n"); } return 0; } |
Output:
Enter the no. of rows: 5
12345
1234
123
12
1
12
123
1234
12345