In this tutorial, i am going to show you how to print hollow sandglass star patterns with the help of for loop and while loop in c programs.
All C Programs to Print Hollow Sandglass Star Pattern
- C Program to Print Hollow Sandglass Star Pattern using For Loop
- C Program to Print Hollow Sandglass Star Pattern using While Loop
C Program to Print Hollow Sandglass Star Pattern using For Loop
#include <stdio.h> int main() { int rows, i, j, k; printf("Enter Hollow Sandglass Star Pattern Rows = "); scanf("%d", &rows); printf("Printing Hollow Sandglass Star Pattern\n"); for (i = 1; i <= rows; i++) { for (j = 1; j < i; j++) { printf(" "); } for (k = i; k <= rows; k++) { if (i == 1 || k == i || k == rows) { printf("* "); } else { printf(" "); } } printf("\n"); } for (i = rows - 1; i >= 1; i--) { for (j = 1; j < i; j++) { printf(" "); } for (k = i; k <= rows; k++) { if (i == 1 || k == i || k == rows) { printf("* "); } else { printf(" "); } } printf("\n"); } }
The result of the above c program; as follow:
Enter Hollow Sandglass Star Pattern Rows = 8 Printing Hollow Sandglass Star Pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
C Program to Print Hollow Sandglass Star Pattern using While Loop
#include <stdio.h> int main() { int rows, i, j, k; printf("Enter Hollow Sandglass Star Pattern Rows = "); scanf("%d", &rows); printf("Printing Hollow Sandglass Star Pattern\n"); i = 1; while (i <= rows) { j = 1; while (j < i) { printf(" "); j++; } k = i; while (k <= rows) { if (i == 1 || k == i || k == rows) { printf("* "); } else { printf(" "); } k++; } printf("\n"); i++; } i = rows - 1; while (i >= 1) { j = 1; while (j < i) { printf(" "); j++; } k = i; while (k <= rows) { if (i == 1 || k == i || k == rows) { printf("* "); } else { printf(" "); } k++; } printf("\n"); i--; } }
The result of the above c program; as follow:
Enter Hollow Sandglass Star Pattern Rows = 8 Printing Hollow Sandglass Star Pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Be First to Comment