Program to check if N is a Concentric Hexagonal Number
Given an integer N, the task is to check if N is a Concentric Hexagonal Numbers or not. If the number N is a Concentric Hexagonal Number then print “Yes” else print “No”.
Concentric Hexagonal Numbers are the number sequence forms a pattern with concentric hexagons, and the numbers denote the number of points required after the N-th stage of the pattern. The first few concentric hexagonal numbers are 0, 1, 6, 13, 24, 37, 54, 73, 96, 121 …
Examples:
Input: N = 6
Output: Yes
Explanation:
Third Concentrichexagonal number is 6.Input: N = 20
Output: No
Approach:
- The Kth term of the Concentric hexagonal number is given as:
Kth Term = (3 * K * K) / 2
- As we have to check that the given number can be expressed as a Concentric hexagonal number or not. This can be checked as:
Here, Kth Term = N
=> (3 * K * K) / 2 = N
=> 3 * K * K – 2 * N = 0
The positive root of this equation is:
K = sqrt((2 * N )/3) - If the value of K calculated using the above formula is an integer, then N is a Concentric Hexagonal Number.
- Else the number N is not a ConcentricHexagonal Number.
Below is the implementation of the above approach:
C++
// C++ program to check if N is a// Concentric Hexagonal Number#include <bits/stdc++.h>usingnamespacestd;// Function to check if the// number is a Concentric hexagonal numberboolisConcentrichexagonal(intN){floatn =sqrt((2 * N) / 3);// Condition to check if the// number is a Concentric// hexagonal numberreturn(n - (int)n) == 0;}// Driver Codeintmain(){intN = 6;// Function callif(isConcentrichexagonal(N)) {cout <<"Yes";}else{cout <<"No";}return0;}Java
// Java program to check if N is a// Concentric Hexagonal NumberclassGFG{// Function to check if the// number is a Concentric hexagonal numberstaticbooleanisConcentrichexagonal(intN){floatn = (float) Math.sqrt((2* N) /3);// Condition to check if the// number is a Concentric// hexagonal numberreturn(n - (int)n) ==0;}// Driver Codepublicstaticvoidmain(String[] args){intN =6;// Function callif(isConcentrichexagonal(N)){System.out.print("Yes");}else{System.out.print("No");}}}// This code is contributed by PrinciRaj1992Python3
# Python3 program to check if N is a# concentric hexagonal numberimportmath# Function to check if the number# is a concentric hexagonal numberdefisConcentrichexagonal(N):n=math.sqrt((2*N)/3)# Condition to check if the# number is a concentric# hexagonal numberreturn(n-int(n))==0# Driver codeN=6ifisConcentrichexagonal(N):print("Yes")else:print("No")# This code is contributed by divyeshrabadiya07C#
// C# program to check if N is a// concentric hexagonal numberusingSystem;classGFG{// Function to check if the number// is a concentric hexagonal numberstaticboolisConcentrichexagonal(intN){floatn = (float) Math.Sqrt((2 * N) / 3);// Condition to check if the// number is a concentric// hexagonal numberreturn(n - (int)n) == 0;}// Driver CodepublicstaticvoidMain(){intN = 6;// Function callif(isConcentrichexagonal(N)){Console.Write("Yes");}else{Console.Write("No");}}}// This code is contributed by Code_MechOutput: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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


