# Centered hexagonal number

Given a number n and the task is to find nth centered hexagonal number. Also find the Centered hexagonal series.
A centered hexagonal number or hex number is centered figure number that represents a hexagon with dot in center and all other dot surrounding in hexagonal form. Nth centered hexagonal number can be calculated by using formula 3n(n – 1) + 1.

Examples :

```Input : n = 2
Output : 7

Input : n = 10
Output : 271
```

## C++

 `// Program to find nth  ` `// centered hexadecimal number. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find centered ` `// hexadecimal number. ` `int` `centeredHexagonalNumber(``int` `n) ` `{ ` `    ``// Formula to calculate nth  ` `    ``// centered hexadecimal number  ` `    ``// and return it into main function. ` `    ``return` `3 * n * (n - 1) + 1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``cout << n << ``"th centered hexagonal number: "``; ` `    ``cout << centeredHexagonalNumber(n); ` `    ``return` `0; ` `} `

## Java

 `// Java Program to find nth ` `// centered hexadecimal number ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to find centered ` `    ``// hexadecimal number ` `    ``static` `int` `centeredHexagonalNumber(``int` `n) ` `    ``{ ` `        ``// Formula to calculate nth  ` `        ``// centered hexadecimal number ` `        ``// and return it into main function ` `        ``return` `3` `* n * (n - ``1``) + ``1``; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``10``; ` `        ``System.out.print(n + ``"th centered "` `+ ` `                       ``"hexagonal number: "``); ` `        ``System.out.println(centeredHexagonalNumber(n)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# Python 3 program to find nth  ` `# centered hexagonal number ` ` `  `# Function to find  ` `# centered hexagonal number ` `def` `centeredHexagonalNumber(n) : ` `     `  `    ``# Formula to calculate  ` `    ``# nth centered hexagonal ` `    ``return` `3` `*` `n ``*` `(n ``-` `1``) ``+` `1` ` `  ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'` `: ` `         `  `    ``n ``=` `10` `    ``print``(n, ``"th centered hexagonal number: "` `                ``, centeredHexagonalNumber(n)) ` ` `  ` `  `# This code is contributed ` `# by 'Akanshgupta' `

## C#

 `// C# Program to find nth  ` `// centered hexadecimal number ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to find centered  ` `    ``// hexadecimal number ` `    ``static` `int` `centeredHexagonalNumber(``int` `n) ` `    ``{ ` `        ``// Formula to calculate nth  ` `        ``// centered hexadecimal number  ` `        ``// and return it into main function ` `        ``return` `3 * n * (n - 1) + 1; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 10; ` `        ``Console.Write(n + ``"th centered "``+  ` `                   ``"hexagonal number: "``); ` `        ``Console.Write(centeredHexagonalNumber(n)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```10th centered hexagonal number: 271
```

Time Complexity : O(1)

Given a number n and the task is to find centered hexagonal series till n.
We can also find centered hexagonal series.

## C++

 `// Program to find the series  ` `// of centered hexadecimal number ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the  ` `// series of centered  ` `// hexadecimal number. ` `void` `centeredHexagonalSeries(``int` `n) ` `{ ` `    ``// Formula to calculate  ` `    ``// nth centered hexadecimal ` `    ``// number. ` `    ``for` `(``int` `i = 1; i <= n; i++) ` `        ``cout << 3 * i * (i - 1) + 1  ` `             ``<< ``" "``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``centeredHexagonalSeries(n); ` `    ``return` `0; ` `} `

## Java

 `// Program to find the series of  ` `// centered hexadecimal number. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `    ``// Function to find the series of  ` `    ``// centered hexadecimal number. ` `    ``static` `void` `centeredHexagonalSeries(``int` `n) ` `    ``{ ` `        ``// Formula to calculate nth  ` `        ``// centered hexadecimal number. ` `        ``for` `(``int` `i = ``1``; i <= n; i++) ` `            ``System.out.print( ``3` `* i *  ` `                            ``(i - ``1``) + ``1` `+ ``" "``); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``10``; ` `        ``centeredHexagonalSeries(n); ` `    ``} ` `} ` ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# Python3 program to find  ` `# nth centered hexagonal number ` ` `  `# Function to find centered hexagonal  ` `# series till n given numbers. ` `def` `centeredHexagonalSeries(n) : ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``) : ` `         `  `        ``# Formula to calculate nth ` `        ``# centered hexagonal series. ` `        ``print``(``3` `*` `i ``*` `(i ``-` `1``) ``+` `1``, end``=``" "``) ` `         `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'` `: ` `     `  `    ``n ``=` `10` `    ``centeredHexagonalSeries(n) ` ` `  `# This code is contributed  ` `# by 'Akanshgupta' `

## C#

 `// C# Program to find the  ` `// series of centered  ` `// hexadecimal number. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to find the  ` `    ``// series of centered  ` `    ``// hexadecimal number. ` `    ``static` `void` `centeredHexagonalSeries(``int` `n) ` `    ``{ ` `        ``// Formula to calculate nth  ` `        ``// centered hexadecimal number. ` `        ``for` `(``int` `i = 1; i <= n; i++) ` `            ``Console.Write( 3 * i *  ` `                         ``(i - 1) + 1 + ``" "``); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 10; ` `        ``centeredHexagonalSeries(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output :

```1 7 19 37 61 91 127 169 217 271
```

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.

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.