In this tutorial, we will write a C Program to display full hourglass patterns using numbers. Before that, you may go through the following topic in C.
Half Diamond number pattern in C
We will write various programs for the left half and right half diamond with different kinds of number patterns. These programs can also be called the arrowhead pattern programs in C.
Right Half Diamond number patter
1. Increasing 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 = 1; i <= rows; i++) { for (j = 1; j <= i; j++) printf("%d ", j); printf("\n"); } for (i = rows - 1; i >= 1; i--) { for (j = 1; j <= i; j++) printf("%d ", j); printf("\n"); } return 0; } |
Output:
Enter the no. of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
2. Mirror 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 34 35 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no of rows: "); scanf("%d", &rows); //upper half for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) printf("%d", j); for (j = i - 1; j >= 1; j--) printf("%d", j); printf("\n"); } //lower half for (i = rows - 1; i >= 1; i--) { for (j = 1; j <= i; j++) printf("%d", j); for (j = i - 1; j >= 1; j--) printf("%d", j); printf("\n"); } return 0; } |
Output:
Enter the no of rows: 5
1
121
12321
1234321
123454321
1234321
12321
121
1
3. Increment in two numbers
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 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); // Upper half for (i = 1; i <= rows; i++) { for (j = 1; j <= (i *2) - 1; j++) printf("%d", j); printf("\n"); } // Lower Half for (i = rows - 1; i >= 1; i--) { for (j = 1; j <= (i *2) - 1; j++) printf("%d", j); printf("\n"); } return 0; } |
Output:
Enter the no. of rows: 5
1
123
12345
1234567
123456789
1234567
12345
123
1
Left half diamond 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 32 33 34 35 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no of rows: "); scanf("%d", &rows); // upper half for (i = 1; i <= rows; i++) { for (j = rows - 1; j >= i; j--) printf(" "); for (j = 1; j <= i; j++) printf("%d", j); printf("\n"); } //lower part for (i = rows - 1; i >= 1; i--) { for (j = i; j < rows; j++) printf(" "); for (j = 1; j <= i; j++) printf("%d", j); printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 | Enter the no of rows: 5 1 12 123 1234 12345 1234 123 12 1 |
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 32 33 34 35 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); // Upper half for (i = 1; i <= rows; i++) { for (j = (i *2); j < (rows *2); j++) printf(" "); for (j = 1; j <= (i *2 - 1); j++) printf("%d", j); printf("\n"); } // Lower Half for (i = rows - 1; i >= 1; i--) { for (j = (i *2); j < (rows *2); j++) printf(" "); for (j = 1; j <= (i *2 - 1); j++) printf("%d", j); printf("\n"); } return 0; } |
Output
1 2 3 4 5 6 7 8 9 10 | Enter the no. of rows: 5 1 123 12345 1234567 123456789 1234567 12345 123 1 |