Given an integer n, find the nth Pentagonal number. The first three pentagonal numbers are 1, 5, and 12 (Please see below diagram).

The n’th pentagonal number P_{n} is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots when the pentagons are overlaid so that they share one vertex [Source Wiki]

**Examples :**

Input:n = 1Output:1Input:n = 2Output:5Input:n = 3Output:12

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 = 5, we get n'th Pentagonal number P_{n}= 3*n*(n-1)/2 + n

Examples:

**Pentagonal Number**

Below are the implementations of the above idea in different programming languages.

## C++

`// C++ program for above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Finding the nth pentagonal number ` `int` `pentagonalNum(` `int` `n) ` `{ ` ` ` `return` `(3 * n * n - n) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `cout << ` `"10th Pentagonal Number is = "` ` ` `<< pentagonalNum(n); ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C

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

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python

`# Python program for finding pentagonal numbers ` `def` `pentagonalNum( n ): ` ` ` `return` `(` `3` `*` `n` `*` `n ` `-` `n)` `/` `2` `#Script Begins ` ` ` `n ` `=` `10` `print` `"10th Pentagonal Number is = "` `, pentagonalNum(n) ` ` ` `#Scripts Ends ` |

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for above approach ` ` ` `// Finding the nth Pentagonal Number ` `function` `pentagonalNum(` `$n` `) ` `{ ` ` ` `return` `(3 * ` `$n` `* ` `$n` `- ` `$n` `) / 2; ` `} ` ` ` `// Driver Code ` `$n` `= 10; ` `echo` `"10th Pentagonal Number is = "` `, ` ` ` `pentagonalNum(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

10th Pentagonal Number is = 145

Reference:

https://en.wikipedia.org/wiki/Polygonal_number

This article is contributed by **Mazhar Imam Khan**. 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to check if N is a Pentagonal Number
- Centered pentagonal number
- Pentagonal Pyramidal Number
- Program to check if N is a Centered Pentagonal Number or not
- Find the sum of the first N Centered Pentagonal Number
- Program to find volume and surface area of pentagonal prism
- Find all numbers up to N which are both Pentagonal and Hexagonal
- Second Pentagonal numbers
- Number of factors of very large number N modulo M where M is any prime number
- Find minimum number to be divided to make a number a perfect square
- How to check if a given number is Fibonacci number?
- Find the smallest number whose digits multiply to a given number n
- Find n'th number in a number system with only 3 and 4
- Build Lowest Number by Removing n digits from a given number
- Minimum number of squares whose sum equals to given number n
- Count number of subsets of a set with GCD equal to a given number
- Count number of ways to divide a number in 4 parts
- Querying maximum number of divisors that a number in a given range has
- Check if a number is a power of another number
- Find the Largest number with given number of digits and sum of digits