# Centered heptagonal number

Given a number** n**, the task is to find n^{th} Centered heptagonal number.

Centered heptagonal number is centered figure number that represents a heptagon with dot in center and all other dot surrounding in heptagonal form. N^{th} centered heptagonal number can be calculated by using formula **(7n ^{2} – 7n + 2) / 2**.

**Examples :**

Input : n = 2 Output : 8 Input : n = 7 Output : 148

Please refer this diagram for pictorial representation.

Below is the implementation :

## C++

`// CPP program to find n-th ` `// Centered heptagonal number ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to find Centered ` `// heptagonal number ` `int` `centered_heptagonal_num(` `long` `int` `n) ` `{ ` ` ` `// Formula to calculate nth ` ` ` `// Centered heptagonal number ` ` ` `return` `(7 * n * n - 7 * n + 2) / 2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `long` `int` `n = 5; ` ` ` `cout << n << ` `"th Centered heptagonal number : "` `; ` ` ` `cout << centered_heptagonal_num(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find n-th Centered ` `// heptagonal number ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find Centered heptagonal ` ` ` `// number ` ` ` `static` `long` `centered_heptagonal_num(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Formula to calculate nth ` ` ` `// Centered heptagonal number ` ` ` `return` `(` `7` `* n * n - ` `7` `* n + ` `2` `) / ` `2` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `long` `n = ` `5` `; ` ` ` `System.out.println( n + ` `"th Centered "` ` ` `+ ` `"heptagonal number : "` ` ` `+ centered_heptagonal_num(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find nth ` `# Centered heptagonal number ` ` ` `# Function to find Centered ` `# heptagonal number ` `def` `centered_heptagonal_num(n): ` ` ` ` ` `# Formula to calculate nth ` ` ` `# Centered heptagonal number ` ` ` `return` `(` `7` `*` `n ` `*` `n ` `-` `7` `*` `n ` `+` `2` `) ` `/` `/` `2` ` ` ` ` `# Driver Code ` `n ` `=` `5` `print` `(` `"%sth Centered heptagonal number : "` `%` `n, ` ` ` `centered_heptagonal_num(n)) ` |

*chevron_right*

*filter_none*

## C#

`//C# program to find n-th Centered ` `// heptagonal number ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find Centered heptagonal ` ` ` `// number ` ` ` `static` `long` `centered_heptagonal_num(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Formula to calculate nth ` ` ` `// Centered heptagonal number ` ` ` `return` `(7 * n * n - 7 * n + 2) / 2; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `long` `n = 5; ` ` ` `Console.WriteLine( n + ` `"th Centered "` ` ` `+ ` `"heptagonal number : "` ` ` `+ centered_heptagonal_num(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n-th ` `// Centered heptagonal number ` ` ` `// Function to find Centered ` `// heptagonal number ` `function` `centered_heptagonal_num(` `$n` `) ` `{ ` ` ` `// Formula to calculate nth ` ` ` `// Centered heptagonal number ` ` ` `return` `(7 * ` `$n` `* ` `$n` `- 7 * ` ` ` `$n` `+ 2) / 2; ` `} ` ` ` `// Driver Code ` `$n` `= 5; ` `echo` `$n` `,` `"th Centered heptagonal number : "` `; ` `echo` `centered_heptagonal_num(` `$n` `); ` ` ` `// This code is contributed by m_kit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5th Centered heptagonal number : 71

## Recommended Posts:

- Heptagonal number
- Centered pentagonal number
- Centered Octahedral number
- Centered tetrahedral number
- Centered triangular number
- Centered Square Number
- Centered tridecagonal number
- Centered Hexadecagonal Number
- Centered dodecahedral number
- Centered Pentadecagonal Number
- Centered Octagonal Number
- Centered cube number
- Centered decagonal number
- Centered nonadecagonal number
- Centered hexagonal number

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.