C Program to Print Hollow Sandglass Star Pattern

Write a 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");
	}
}
C Program to Print Hollow Sandglass Star Pattern

This C example uses the while loop to print the hollow sandglass star pattern.

#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--;
	}
}
Enter Hollow Sandglass Star Pattern Rows = 11
Printing Hollow Sandglass Star Pattern
* * * * * * * * * * * 
 *                 * 
  *               * 
   *             * 
    *           * 
     *         * 
      *       * 
       *     * 
        *   * 
         * * 
          * 
         * * 
        *   * 
       *     * 
      *       * 
     *         * 
    *           * 
   *             * 
  *               * 
 *                 * 
* * * * * * * * * * * 

This C program displays the hollow sandglass pattern of stars using a do 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;
	do
	{
		j = 1;
		do
		{
			printf(" ");
		} while (j++ <= i);
		k = i;
		do
		{
			if (i == 1 || k == i || k == rows)
			{
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		} while (++k <= rows);
		printf("\n");
	} while (++i <= rows);

	i = rows - 1;
	do
	{
		j = 1;
		do
		{
			printf(" ");
		} while (j++ <= i);
		k = i;
		do
		{
			if (i == 1 || k == i || k == rows)
			{
				printf("* ");
			}
			else
			{
				printf("  ");
			}
		} while (++k <= rows);
		printf("\n");
	} while (--i >= 1);
}
Enter Hollow Sandglass Star Pattern Rows = 14
Printing Hollow Sandglass Star Pattern
  * * * * * * * * * * * * * * 
   *                       * 
    *                     * 
     *                   * 
      *                 * 
       *               * 
        *             * 
         *           * 
          *         * 
           *       * 
            *     * 
             *   * 
              * * 
               * 
              * * 
             *   * 
            *     * 
           *       * 
          *         * 
         *           * 
        *             * 
       *               * 
      *                 * 
     *                   * 
    *                     * 
   *                       * 
  * * * * * * * * * * * * * * 

This C example code allows the user to enter the character and prints the hollow sandglass pattern of a given character.

#include <stdio.h>

void HollowSandglassPattern(int rows, char ch);

int main()
{
	int rows;
	char ch;

	printf("Enter Character for Hollow Sandglass Pattern = ");
	scanf("%c", &ch);

	printf("Enter Hollow Sandglass Star Pattern Rows = ");
	scanf("%d", &rows);

	printf("Printing Hollow Sandglass Star Pattern\n");
	HollowSandglassPattern(rows, ch);
}

void HollowSandglassPattern(int rows, char ch)
{
	int i, j, k;

	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("%c ", ch);
			}
			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("%c ", ch);
			}
			else
			{
				printf("  ");
			}
		}
		printf("\n");
	}
}
Enter Character for Hollow Sandglass Pattern = $
Enter Hollow Sandglass Star Pattern Rows = 16
Printing Hollow Sandglass Star Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
 $                           $ 
  $                         $ 
   $                       $ 
    $                     $ 
     $                   $ 
      $                 $ 
       $               $ 
        $             $ 
         $           $ 
          $         $ 
           $       $ 
            $     $ 
             $   $ 
              $ $ 
               $ 
              $ $ 
             $   $ 
            $     $ 
           $       $ 
          $         $ 
         $           $ 
        $             $ 
       $               $ 
      $                 $ 
     $                   $ 
    $                     $ 
   $                       $ 
  $                         $ 
 $                           $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.