Tuesday, 13 August 2013

Hexagon

Hexagon

In this post of shapes in c, you will find the code to draw hexagon in C++ using "*". 

 Code:

copy the code from here.
/*         Join us at
 http://shapesinc.blogspot.com 
     for more codes of shapes  */
#include<iostream>
using namespace std;
int main()
{
cout << "Join http://shapesinc.blogspot.com for more codes of shapes\n\n";
 cout<<"CAUTION:   The input should not be other than numerical values \n\n\a";  
int length=0;
cout << "Enter the size of the hexagon\n";
cin>>length;
for(int i=0, k=length, l=2*length-1; i<length; i++, k--, l++)
{
for(int j=0; j<3*length; j++)
{
if(j>=k && j<=l)
cout << "*";
else
cout << " ";
}
cout<<endl;
}
for(int i=0; i<length; i++)
{
for(int j=0; j<3*length; j++)
cout << "*";
cout<<endl;
}
for(int i=0, k=1, l=3*length-2; i<length; i++, k++, l--)
{
for(int j=0; j<3*length; j++)
{
if(j>=k && j<=l)
cout << "*";
else
cout << " ";
}
cout<<endl;
}
}

Output:


Hexagon_Shapes_In_C

1 comments: