Skip to content
Related Articles
Program to check if N is a Centered Hexagonal Number
• Last Updated : 07 Apr, 2021

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 number are 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 = 7
Output: Yes
Explanation:
Second Centered hexagonal number is 7.

Input: N = 20
Output: No

Approach:

• The Kth term of the Centered hexagonal number is given as • 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 using namespace std; // Function to check that the// number is a Centered hexagonal numberbool 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 Codeint main(){    int N = 7;     // Function call    if (isCenteredhexagonal(N)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}

## Java

 // Java program for the above approachclass GFG{ // Function to check that the// number is a Centered hexagonal numberstatic 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 Codepublic 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 approachimport math # Function to check that the number# is a centered hexagonal numberdef 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 CodeN = 7 if isCenteredhexagonal(N):    print("Yes")else :    print("No") # This code is contributed by ishayadav181

## C#

 // C# program for the above approachusing System; class GFG{ // Function to check that the number// is a centered hexagonal numberstatic 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 Codepublic 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

 
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

My Personal Notes arrow_drop_up