Program for Hexagonal Pattern

Given a positive integer n, print a hexagonal pattern using start with each sides containing n stars.
Example :

Input : 4
Output :   
   *
  * *
 *   *
*     *
*     *
*     *
*     *
 *   *
  * *
   *
// Hexagon pattern with each side
// having n stars where n is the user input.

Input : 6
Output :   
     *
    * *
   *   *
  *     *
 *       *
*         *
*         *
*         *
*         *
*         *
*         *
 *       *
  *     *
   *   *
    * *
     *

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print hexagonal pattern
  
class GFG
{
    static 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))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\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)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\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))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
      
    // Driver code
    public static void main (String[] args)
    {
        // length of a side
        // n >= 2
        int n = 3;
        printHexagon(n);
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to 
# print hexagonal pattern
def printHexagon(n):
  
    l = 2 * n - 1
  
    # Print upper part 
    for i in range(0, n):
      
        elem = i + n
  
        # loop for printing the
        # whitespaces and the star
        for k in range(0, elem):
          
            # prints the star
            if ((k == n + i - 1) or 
                (k == n - i - 1)):
                print("*", end = "")
            else:
                print(" ", end = "")
          
        print("")
      
    # Print middle part 
    # Here we print two 
    # vertical lines
    for m in range(0, n - 2):
      
        for j in range(0, l):
              
            if (j == 0 or j == l - 1):
                print("*", end = "")
            else:
                print(" ", end = "")
          
        print("")
      
    # Print Lower part 
    r = n - 1
    for h in range(r, -1, -1):
      
        elem = h + n
        for k in range(0, elem):
          
            if ((k == n + h - 1) or 
                (k == n - h - 1)):
                print("*", end = "")
            else:
                print(" ", end = "")
                  
        print("")
      
# Driver Code
# length of a side
# n >= 2
n = 3
printHexagon(n)
  
# This code is contributed
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print hexagonal pattern
using System;
class GFG
{
    static 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))
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                    Console.WriteLine();
        }
      
        /**** 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)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                   Console.WriteLine();
        }
      
        /**** 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))
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
                    Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main ()
    {
        // length of a side
        // n >= 2
        int n = 3;
        printHexagon(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to 
// print hexagonal pattern
  
function printHexagon($n)
{
    $l = 2 * $n - 1;
  
    // Print upper part 
    for ($i = 0; $i < $n; $i++)
    {
        $elem = $i + $n;
  
        // loop for printing the
        // whitespaces and the star
        for ($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 ($m = 0; $m < $n - 2; $m++)
    {
        for ($j = 0; $j < $l; $j++)
        {
            if ($j == 0 || $j == $l - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
  
    // Print Lower part 
    $r = $n - 1;
    for ($h = $r; $h >= 0; $h--)
    {
        $elem = $h + $n;
        for ($k = 0; $k < $elem; $k++)
        {
            if (($k == $n + $h - 1) || 
                ($k == $n - $h - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}
  
// Driver Code
  
// length of a side n >= 2
$n = 3;
printHexagon($n);
  
// This code is contributed by mits 
?>

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.