# Hexagonal Number

Given an integer n, the task is to find the n’th hexagonal number . The n’th hexagonal number Hn is the number of distinct dots in a pattern of dots consisting of the outlines of regular hexagons with sides up to n dots, when the hexagons are overlaid so that they share one vertex.{Source : wiki}

Input : n = 2 Output : 6 Input : n = 5 Output : 45 Input : n = 7 Output : 91

In general, a polygonal number (triangular number, square number, etc) is a number represented as dots or pebbles arranged in the shape of a regular polygon. The first few pentagonal numbers are: 1, 5, 12, etc.

If s is the number of sides in a polygon, the formula for the nth s-gonal number P (s, n) is

nth s-gonal number P(s, n) = (s - 2)n(n-1)/2 + n If we put s = 6, we get n'th Hexagonal number H_{n}= 2(n*n)-n = n(2n - 1)

## C/C++

`// C program for above approach ` `#include <stdio.h> ` `#include <stdlib.h> ` ` ` `// Finding the nth Hexagonal Number ` `int` `hexagonalNum(` `int` `n) ` `{ ` ` ` `return` `n*(2*n - 1); ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `printf` `(` `"10th Hexagonal Number is = %d"` `, ` ` ` `hexagonalNum(n)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for above approach ` `class` `Hexagonal ` `{ ` ` ` `int` `hexagonalNum(` `int` `n) ` ` ` `{ ` ` ` `return` `n*(` `2` `*n - ` `1` `); ` ` ` `} ` `} ` ` ` `public` `class` `GeeksCode ` `{ ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `Hexagonal obj = ` `new` `Hexagonal(); ` ` ` `int` `n = ` `10` `; ` ` ` `System.out.printf(` `"10th Hexagonal number is = "` ` ` `+ obj.hexagonalNum(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program for finding pentagonal numbers ` `def` `hexagonalNum( n ): ` ` ` `return` `n` `*` `(` `2` `*` `n ` `-` `1` `) ` ` ` `# Driver code ` `n ` `=` `10` `print` `"10th Hexagonal Number is = "` `, hexagonalNum(n) ` |

*chevron_right*

*filter_none*

## C#

`// C# program for above approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `hexagonalNum(` `int` `n) ` ` ` `{ ` ` ` `return` `n * (2 * n - 1); ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` ` ` `int` `n = 10; ` ` ` ` ` `Console.WriteLine(` `"10th Hexagonal"` ` ` `+ ` `" number is = "` `+ hexagonalNum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for above approach ` ` ` `// Finding the nth Hexagonal Number ` `function` `hexagonalNum(` `$n` `) ` `{ ` ` ` `return` `$n` `* (2 * ` `$n` `- 1); ` `} ` ` ` `// Driver program to test above function ` `$n` `= 10; ` `echo` `(` `"10th Hexagonal Number is "` `. ` ` ` `hexagonalNum(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

10th Hexagonal Number is = 190

Reference:https://en.wikipedia.org/wiki/Hexagonal_number

This article is contributed by **Nishant_Singh(pintu)**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Centered hexagonal number
- Surface Area and Volume of Hexagonal Prism
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Check if a number is divisible by all prime divisors of another number
- Find the largest good number in the divisors of given number N
- Count number of digits after decimal on dividing a number
- Count Number of animals in a zoo from given number of head and legs
- Find the smallest number whose digits multiply to a given number n
- Print a number strictly less than a given number such that all its digits are distinct.
- Super Ugly Number (Number whose prime factors are in given set)
- Querying maximum number of divisors that a number in a given range has
- Program to Convert Octal Number to Binary Number
- Number of digits to be removed to make a number divisible by 3
- Build Lowest Number by Removing n digits from a given number