Program to check if N is a Centered Hexagonal Number Last Updated : 18 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 asK^{th} Term = {3*K^{2} - 3*K + 2} As we have to check that the given number can be expressed as a Centered hexagonal number or not. This can be checked as: => N = {3*K^{2} - 3*K + 2} => K = \frac{3 + \sqrt{12*N - 3}}{6} 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 Time Complexity: O(logN) because it is using inbuilt sqrt functionAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to check if N is a Centered Hexagonal Number kartik Follow Improve Article Tags : Mathematical DSA Practice Tags : Mathematical Similar Reads Program to check if N is a Centered heptagonal number Given an integer N, the task is to check if it is a Centered heptagonal number or not. Centered heptagonal number is centered figure number that represents a heptagon with dot in center and all other dot surrounding in heptagonal form..The first few Centered heptagonal number are 1, 8, 22, 43, 71, 1 4 min read Program to check if N is a Centered Hexadecagonal Number Given a number N, the task is to check if N is a Centered Hexadecagonal Number or not. If the number N is a Centered Hexadecagonal Number then print "Yes" else print "No". Centered Hexadecagonal Number represents a dot in the centre and other dots around it in successive Hexadecagonal(16 sided polyg 4 min read Program to check if N is a Centered Decagonal Number Given an integer N, the task is to check if N is a Centered Decagonal Number or not. If the number N is a Centered Decagonal Number then print "Yes" else print "No". Centered Decagonal Number is centered figurative number that represents a decagon with dot in center and all other dot surrounding it 4 min read Program to check if N is a Centered dodecagonal number Given an integer N, the task is to check if N is a Centered Dodecagonal Number or not. If the number N is a Centered Dodecagonal Number then print "Yes" else print "No". Centered Dodecagonal Number represents a dot in the center and other dots surrounding it in successive Dodecagonal Number(12 sided 4 min read Program to check if N is a Heptagonal Number Given an integer N, the task is to check if N is a Heptagonal Number or not. If the number N is an Heptagonal Number then print "Yes" else print "No". Heptagonal Number represents Heptagon and belongs to a figurative number. Heptagonal has seven angles, seven vertices, and seven-sided polygon. The f 7 min read Program to check if N is a Hexadecagonal Number Given an integer N, the task is to check if N is a Hexadecagonal Number or not. If the number N is an Hexadecagonal Number then print "Yes" else print "No". Hexadecagonal Number is class of figurate number and a perfect squares. It has 16-sided polygon called Hexadecagon or Hexakaidecagon. The nth H 4 min read Program to check if N is a Centered Cubic Number Given a number N, the task is to check if N is a centered cubic number or not. A centered cubic number counts the number of points which are formed by a point that is surrounded by concentric cubical layers in 3D with i2 points on the square faces of the i-th layer. The first few Centered cube numbe 7 min read Program to check if N is a Icosihexagonal Number Given an integer N, the task is to check if it is a Icosihexagonal number or not. Icosihexagonal number is class of figurate number. It has 26 â sided polygon called Icosihexagon. The N-th Icosihexagonal number countâs the 26 number of dots and all other dots are surrounding with a common sharing co 4 min read Program to check if N is a Decagonal Number Given a number N, the task is to check if N is a Decagonal Number or not. If the number N is an Decagonal Number then print "Yes" else print "No". Decagonal Number is a figurate number that extends the concept of triangular and square numbers to the decagon (10-sided polygon). The nth decagonal numb 4 min read Program to check if N is a Hendecagonal Number Given an integer N, the task is to check if N is a Hendecagonal Number or not. If the number N is a Hendecagonal Number then print "Yes" else print "No" Hendecagonal Number is a figurate number that extends the concept of triangular and square numbers to the decagon(11-sided polygon). The nth hendec 4 min read Like