# Find the sum of the first Nth Centered Hexadecagonal Number

Given a number N, the task is to find the sum of the first N Centered Hexadecagonal Number.

The first few Centered Hexadecagonal Numbers are 1, 17, 49, 97, 161, 241 …

Examples:

Input: N = 3
Output: 67
Explanation:
1, 17 and 49 are the first three centered Hexadecagonal numbers.

Input: N = 5
Output: 325

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Initially, we need to create a function which will help us to calculate the Nth Centered Hexadecagonal number.
2. Now, we run a loop starting from 1 to N, to find ith Centered Hexadecagonal number.
4. Finally, display the sum of 1st N Centered Hexadecagonal numbers.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the sum of the first  ` `// N centred hexadecagonal numbers ` `#include   ` `using` `namespace` `std;  ` ` `  `// Centered_Hexadecagonal  ` `// number function ` `int` `Centered_Hexadecagonal_num(``int` `n)  ` `{ ` `     `  `    ``// Formula to calculate nth  ` `    ``// Centered_Hexadecagonal  ` `    ``// number & return it into ` `    ``// main function.  ` `    ``return` `(8 * n * n - 8 * n + 1); ` `} ` ` `  `// Function to find the sum of the first ` `// N centered hexadecagonal number ` `int` `sum_Centered_Hexadecagonal_num(``int` `n) ` `{ ` `     `  `    ``// Variable to store the sum ` `    ``int` `summ = 0; ` `     `  `    ``// Loop to iterate through the ` `    ``// first N numbers ` `    ``for``(``int` `i = 1; i < n + 1; i++) ` `    ``{ ` `         `  `       ``// Finding the sum ` `       ``summ += Centered_Hexadecagonal_num(i); ` `    ``} ` `    ``return` `summ; ` `} ` ` `  `// Driver code  ` `int` `main()  ` `{  ` `    ``int` `n = 5; ` `     `  `    ``// Display first Nth  ` `    ``// Centered_Hexadecagonal number ` `    ``cout << sum_Centered_Hexadecagonal_num(n); ` `}  ` ` `  `// This code is contributed by coder001 `

## Java

 `// Java program to find the sum of the first  ` `// N centred hexadecagonal numbers  ` ` `  `class` `GFG{ ` `     `  `// Centered_Hexadecagonal  ` `// number function  ` `public` `static` `int` `Centered_Hexadecagonal_num(``int` `n)  ` `{  ` `         `  `    ``// Formula to calculate nth  ` `    ``// Centered_Hexadecagonal  ` `    ``// number & return it into  ` `    ``// main function.  ` `    ``return` `(``8` `* n * n - ``8` `* n + ``1``);  ` `}  ` `     `  `// Function to find the sum of the first  ` `// N centered hexadecagonal number  ` `public` `static` `int` `sum_Centered_Hexadecagonal_num(``int` `n)  ` `{  ` `         `  `    ``// Variable to store the sum  ` `    ``int` `summ = ``0``;  ` `         `  `    ``// Loop to iterate through the  ` `    ``// first N numbers  ` `    ``for``(``int` `i = ``1``; i < n + ``1``; i++)  ` `    ``{  ` `         `  `       ``// Finding the sum  ` `       ``summ += Centered_Hexadecagonal_num(i);  ` `    ``}  ` `    ``return` `summ;  ` `}  ` ` `  `// Driver Code     ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``5``;  ` `     `  `    ``// Display first Nth  ` `    ``// Centered_Hexadecagonal number  ` `    ``System.out.println(sum_Centered_Hexadecagonal_num(n)); ` `} ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

## Python3

 `# Python3 program to find the sum of  ` `# the first N centred  ` `# hexadecagonal numbers ` ` `  `# Centered_Hexadecagonal  ` `# number function ` `def` `Centered_Hexadecagonal_num(n):  ` `    ``# Formula to calculate   ` `    ``# nth Centered_Hexadecagonal  ` `    ``# number & return it  ` `    ``# into main function.  ` `    ``return` `(``8` `*` `n ``*` `n ``-`  `            ``8` `*` `n ``+` `1``) ` `     `  `   `  `# Function to find the  ` `# sum of the first N ` `# Centered Hexadecagonal  ` `# number  ` `def` `sum_Centered_Hexadecagonal_num(n) :  ` `     `  `    ``# Variable to store the  ` `    ``# sum ` `    ``summ ``=` `0` `     `  `    ``# Loop to iterate through the  ` `    ``# first N numbers ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` ` `  `        ``# Find the sum ` `        ``summ ``+``=` `Centered_Hexadecagonal_num(i) ` `     `  `    ``return` `summ ` `   `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'` `:  ` `           `  `    ``n ``=` `5` `     `  `    ``# display first Nth  ` `    ``# Centered_Hexadecagonal number ` `    ``print``(sum_Centered_Hexadecagonal_num(n))  `

## C#

 `// C# program to find the sum of the first  ` `// N centred hexadecagonal numbers  ` `using` `System; ` ` `  `class` `GFG{ ` `     `  `// Centered_Hexadecagonal  ` `// number function  ` `public` `static` `int` `Centered_Hexadecagonal_num(``int` `n)  ` `{  ` `         `  `    ``// Formula to calculate nth  ` `    ``// Centered_Hexadecagonal  ` `    ``// number & return it into  ` `    ``// main function.  ` `    ``return` `(8 * n * n - 8 * n + 1);  ` `}  ` `     `  `// Function to find the sum of the first  ` `// N centered hexadecagonal number  ` `public` `static` `int` `sum_Centered_Hexadecagonal_num(``int` `n)  ` `{  ` `         `  `    ``// Variable to store the sum  ` `    ``int` `summ = 0;  ` `         `  `    ``// Loop to iterate through the  ` `    ``// first N numbers  ` `    ``for``(``int` `i = 1; i < n + 1; i++)  ` `    ``{  ` `        `  `       ``// Finding the sum  ` `       ``summ += Centered_Hexadecagonal_num(i);  ` `    ``}  ` `    ``return` `summ;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 5;  ` `     `  `    ``// Display first Nth  ` `    ``// Centered_Hexadecagonal number  ` `    ``Console.Write(sum_Centered_Hexadecagonal_num(n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```325
```

Time Complexity: O(N)

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.

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.

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.