Program for Hexagonal Pattern

06 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// 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 Codeint main(){    // length of a side    // n >= 2    int n = 3;    printHexagon(n);    return 0;}
// 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.
# Python3 program to# print hexagonal patterndef 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 >= 2n = 3printHexagon(n) # This code is contributed# by Smitha
// C# program to print hexagonal patternusing 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.
<?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?>
<script>      // JavaScript program to print hexagonal pattern      function printHexagon(n)      {        var l = 2 * n - 1;         /**** Print upper part *****/        for (var i = 0; i < n; i++)        {          var elem = i + n;           // loop for printing the          // whitespaces and the star          for (var k = 0; k < elem; k++)          {                       // prints the star            if (k == n + i - 1 || k == n - i - 1) document.write("*");            else document.write("  ");          }          document.write("<br>");        }         /**** Print middle part *****/        // Here we print two vertical lines        for (var m = 0; m < n - 2; m++)        {          for (var j = 0; j < l; j++)          {            if (j == 0 || j == l - 1) document.write("*");            else document.write("  ");          }          document.write("<br>");        }         /**** Print Lower part *****/        var r = n - 1;        for (var h = r; h >= 0; h--)        {          var elem = h + n;          for (var k = 0; k < elem; k++)          {            if (k == n + h - 1 || k == n - h - 1) document.write("*");            else document.write("  ");          }          document.write("<br>");        }      }       // Driver Code      // length of a side      // n >= 2      var n = 3;      printHexagon(n);             // This code is contributed by rdtank.    </script>
Output :  
  *
 * *
*   *
*   *
*   *
 * *
  *
 
Mithun Kumar
Smitha Dinesh Semwal
rdtank
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
How JVM Works - JVM Architecture?
Python program to add two numbers
Variables in Java
Data types in Java
Difference between Abstract Class and Interface in Java