Given an integer **N**, the task is to check if **N** is a Centered Hexagonal Number or not. If the number **N** is a Centered Hexagonal Number then print “Yes” else print “No”.

Centered hexagonal numberare figurate numbers and are in the form of the Hexagon. The Centered Hexagonal number is different from Hexagonal Number because it contains one element at the center..The first few Centered hexagonal numbers are 1, 7, 19, 37, 61, 91, 127 …

**Examples:**

Input:N = 7Output:YesExplanation:

Second Centered hexagonal number is 7.

Input:N = 20Output:No

**Approach:**

- The
**K**term of the Centered hexagonal number is given as^{th} - As we have to check that the given number can be expressed as a Centered hexagonal number or not. This can be checked as:

=>

=>

- If the value of
**K**calculated using the above formula is an integer, then**N**is a Centered Hexagonal Number. - Else the number
**N**is not a Centered Hexagonal Number.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check that the` `// number is a Centered hexagonal number` `bool` `isCenteredhexagonal(` `int` `N)` `{` ` ` `float` `n` ` ` `= (3 + ` `sqrt` `(12 * N - 3))` ` ` `/ 6;` ` ` `// Condition to check if the` ` ` `// number is a Centered hexagonal number` ` ` `return` `(n - (` `int` `)n) == 0;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 7;` ` ` `// Function call` ` ` `if` `(isCenteredhexagonal(N)) {` ` ` `cout << ` `"Yes"` `;` ` ` `}` ` ` `else` `{` ` ` `cout << ` `"No"` `;` ` ` `}` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG{` `// Function to check that the` `// number is a Centered hexagonal number` `static` `boolean` `isCenteredhexagonal(` `int` `N)` `{` ` ` `float` `n = (` `float` `)((` `3` `+ Math.sqrt(` `12` `* N - ` `3` `)) / ` `6` `);` ` ` `// Condition to check if the` ` ` `// number is a Centered hexagonal number` ` ` `return` `(n - (` `int` `)n) == ` `0` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `7` `;` ` ` `// Function call` ` ` `if` `(isCenteredhexagonal(N))` ` ` `{` ` ` `System.out.print(` `"Yes"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `System.out.print(` `"No"` `);` ` ` `}` `}` `}` `// This code is contributed by sapnasingh4991` |

## Python3

`# Python3 program for the above approach` `import` `math` `# Function to check that the number` `# is a centered hexagonal number` `def` `isCenteredhexagonal(N):` ` ` ` ` `n ` `=` `(` `3` `+` `math.sqrt(` `12` `*` `N ` `-` `3` `)) ` `/` `6` ` ` ` ` `# Condition to check if the number` ` ` `# is a centered hexagonal number` ` ` `return` `(n ` `-` `int` `(n)) ` `=` `=` `0` `# Driver Code` `N ` `=` `7` `if` `isCenteredhexagonal(N):` ` ` `print` `(` `"Yes"` `)` `else` `:` ` ` `print` `(` `"No"` `)` `# This code is contributed by ishayadav181` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to check that the number` `// is a centered hexagonal number` `static` `bool` `isCenteredhexagonal(` `int` `N)` `{` ` ` `float` `n = (` `float` `)((3 + Math.Sqrt(12 * N -` ` ` `3)) / 6);` ` ` `// Condition to check if the number` ` ` `// is a centered hexagonal number` ` ` `return` `(n - (` `int` `)n) == 0;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `N = 7;` ` ` `// Function call` ` ` `if` `(isCenteredhexagonal(N))` ` ` `{` ` ` `Console.Write(` `"Yes"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `Console.Write(` `"No"` `);` ` ` `}` `}` `}` `// This code is contributed by amal kumar choubey` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to check that the` `// number is a Centered hexagonal number` `function` `isCenteredhexagonal(N)` `{` ` ` `let n = parseInt((3 + Math.sqrt(12 * N - 3)) / 6);` ` ` `// Condition to check if the` ` ` `// number is a Centered hexagonal number` ` ` `return` `(n - parseInt(n)) == 0;` `}` `// Driver Code` `let N = 7;` `// Function call` `if` `(isCenteredhexagonal(N))` `{` ` ` `document.write(` `"Yes"` `);` `}` `else` `{` ` ` `document.write(` `"No"` `);` `}` `// This code is contributed by souravmahato348` `</script>` |

**Output:**

Yes

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live**