Open In App

# 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++

 `// C++ program to print hexagonal pattern``#include `` ` `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;``}`

## Java

 `// 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.`

## Python 3

 `# 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`

## C#

 `// 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.`

## PHP

 `= 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 ``?>`

## Javascript

 ``

Output :

```  *
* *
*   *
*   *
*   *
* *
*```

Time complexity: O(n^2) for given input n

Auxiliary space: O(1)