C++ Program to Print Hollow Sandglass Star Pattern

Write a C++ program to print hollow sandglass star pattern using for loop.

#include<iostream>
using namespace std;

int main()
{
	int rows, i, j, k;

	cout << "Enter Hollow Sandglass Star Pattern Rows = ";
	cin >> rows;

	cout << "Printing Hollow Sandglass Star Pattern\n";

	for (i = 1; i <= rows; i++)
	{
		for (j = 1; j < i; j++)
		{
			cout << " ";
		}
		for (k = i; k <= rows; k++)
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << "* ";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << "\n";
	}

	for (i = rows - 1; i >= 1; i--)
	{
		for (j = 1; j < i; j++)
		{
			cout << " ";
		}
		for (k = i; k <= rows; k++)
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << "* ";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << "\n";
	}
}
C++ Program to Print Hollow Sandglass Star Pattern

This C++ program displays the hollow sandglass pattern of stars using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int rows, i, j, k;

	cout << "Enter Hollow Sandglass Star Pattern Rows = ";
	cin >> rows;

	cout << "Printing Hollow Sandglass Star Pattern\n";
	i = 1;
	while (i <= rows)
	{
		j = 1;
		while (j < i)
		{
			cout << " ";
			j++;
		}
		k = i;
		while (k <= rows)
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << "* ";
			}
			else
			{
				cout << "  ";
			}
			k++;
		}
		cout << "\n";
		i++;
	}

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

C++ program to print hollow sandglass star pattern using a do while loop

#include<iostream>
using namespace std;

int main()
{
	int rows, i, j, k;

	cout << "Enter Hollow Sandglass Star Pattern Rows = ";
	cin >> rows;

	cout << "Printing Hollow Sandglass Star Pattern\n";
	i = 1;
	do
	{
		j = 1;
		do
		{
			cout << " ";
		} while (j++ <= i);
		k = i;
		do
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << "* ";
			}
			else
			{
				cout << "  ";
			}
		} while (++k <= rows);
		cout << "\n";
	} while (++i <= rows);

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

In this C++ example, HollowSandglassPattern function allows user to enter the character and prints the hollow sandglass pattern of the given character.

#include<iostream>
using namespace std;

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

	for (i = 1; i <= rows; i++)
	{
		for (j = 1; j < i; j++)
		{
			cout << " ";
		}
		for (k = i; k <= rows; k++)
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << ch << " ";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << "\n";
	}

	for (i = rows - 1; i >= 1; i--)
	{
		for (j = 1; j < i; j++)
		{
			cout << " ";
		}
		for (k = i; k <= rows; k++)
		{
			if (i == 1 || k == i || k == rows)
			{
				cout << ch << " ";
			}
			else
			{
				cout << "  ";
			}
		}
		cout << "\n";
	}
}

int main()
{
	int rows;
	char ch;

	cout << "Enter Hollow Sandglass Star Pattern Rows = ";
	cin >> rows;

	cout << "Enter Character for Hollow Sandglass Pattern = ";
	cin >> ch;

	cout << "Printing Hollow Sandglass Star Pattern\n";
	HollowSandglassPattern(rows, ch);
}
Enter Hollow Sandglass Star Pattern Rows = 15
Enter Character for Hollow Sandglass Pattern = $
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.