C++ Program to Draw Hexagon Using Star Pattern on Command Line

You are currently viewing C++ Program to Draw Hexagon Using Star Pattern on Command Line

C++ Program to Draw Hexagon Using Star Pattern on Command Line

// C++ program to print hexagonal pattern
#include <stdio.h>
 
void printHexagon(int n)
{
int l = 2 * n - 1;
 
/**** Print upper part *****/
for (int i = 0; i < n; i++)
{
int elem = i + n;
 
// loop for printing the
// whitespaces and the star
for (int k = 0; k < elem; k++)
{
// prints the star
if ((k == n + i - 1) || (k == n - i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
 
/**** Print middle part *****/
// Here we print two vertical lines
for (int m = 0; m < n - 2; m++)
{
for (int j = 0; j < l; j++)
{
if (j == 0 || j == l - 1)
printf("*");
else
printf(" ");



}
printf("\n");
}
 
/**** Print Lower part *****/
int r = n - 1;
for (int h = r; h >= 0; h--)
{
int elem = h + n;
for (int k = 0; k < elem; k++)
{
if ((k == n + h - 1) || (k == n - h - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
 
// Driver Code
int main()
{
// length of a side
// n >= 2
int n = 3;
printHexagon(n);
return 0;
}

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply