Program to print hollow sandglass star pattern in c; Through this tutorial, we will learn how to print hollow sandglass star patterns using for loop and while loop in c programs.
Programs to Print Hollow Sandglass Star Pattern in C
- 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 Output 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 Output of the above c program; as follow:
Enter Hollow Sandglass Star Pattern Rows = 8
Printing Hollow Sandglass Star Pattern
* * * * * * * *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* * * * * * * *