Draw Hexagon Shape using C++

Below is the code to draw hexagon shape in C++. Hexagon is a shape having 6 sides.

Code:

#include<iostream>
using namespace std;
int main()
{
	int n,i,j,k;
	cout<<"Enter Size : ";
	cin>>n;
	if(n%2!=0)
	n++;
	cout<<"\n\t";
	for(i=1;i<=(n+1)/2;i++)
	{
		for(j=n;j>=i;j--)
		cout<<"  ";
		for(k=1;k<=2*i-1;k++)
		cout<<"* ";
		cout<<"\n\t";
	}
	for(i=1;i<=n/2;i++)
	{
		for(k=n;k>=n/2;k--)
		cout<<"  ";
		for(j=1;j<=n-1;j++)
		cout<<"* ";
		cout<<"\n\t";
	}
		for(i=(n+1)/2;i>=1;i--)
	{
		for(j=n;j>=i;j--)
		cout<<"  ";
		for(k=1;k<=2*i-1;k++)
		cout<<"* ";
		cout<<"\n\t";
	}
}

Output:

Enter Size : 7

                        *
                      * * *
                    * * * * *
                  * * * * * * *
                  * * * * * * *
                  * * * * * * *
                  * * * * * * *
                  * * * * * * *
                  * * * * * * *
                    * * * * *
                      * * *
                        *

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *