Thursday, October 9, 2025
HomeData Modelling & AIProgram to check if N is a Hexagonal Number or not

Program to check if N is a Hexagonal Number or not

Given a number N, check if it is Hexagonal or not. If it is then print “Yes” otherwise output “No”.

Examples: 

Input: N = 6 
Output: Yes 
Explanation: 
6 is the second hexagonal number.

Input: N = 14 
Output: No 
Explanation: 
The second hexagonal number is 6 while the third hexagonal number is 15. Hence 14 is not a hexagonal number. 

Approach: To solve the problem mentioned above we have to note that the nth hexagonal Number is given by the formula: H(n) = n * (2n – 1). The formula indicates that the n-th hexagonal number depends quadratically on n. Therefore, find the positive integral root of N = H(n) equation.

Therefore, H(n) = nth hexagonal number and N is the given Number. Hence solve the following equation:

Here, H(n) = N 
=> n * (2n – 1) = N 
=> 2 * n * n – n – N = 0 
The positive root of this equation is: 
n = (1 + sqrt(8 N + 1)) / 4 
 

After obtaining the value for n, check if it is an integer or not where n is an integer if n – floor(n) is 0.

Below is the implementation of the above approach: 

C++




// C++ Program to check
// if N is a Hexagonal Number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check
// if number is hexagonal
bool isHexagonal(int N)
{
    float val = 8 * N + 1;
 
    float x = 1 + sqrt(val);
 
    // Calculate the value for n
    float n = (x) / 4;
 
    // Check if n - floor(n)
    // is equal to 0
    if ((n - (int)n) == 0)
        return true;
 
    else
        return false;
}
 
// Driver code
int main()
{
    int N = 14;
 
    if (isHexagonal(N) == true)
        cout << "Yes" << endl;
 
    else
        cout << "No" << endl;
 
    return 0;
}


Java




// Java program to check
// if N is a hexagonal number
import java.util.*;
 
class GFG {
 
// Function to check
// if number is hexagonal
static boolean isHexagonal(int N)
{
    float val = 8 * N + 1;
 
    float x = 1 + (float)Math.sqrt(val);
 
    // Calculate the value for n
    float n = (x) / 4;
 
    // Check if n - floor(n)
    // is equal to 0
    if ((n - (int)n) == 0)
        return true;
 
    else
        return false;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 14;
 
    if (isHexagonal(N) == true)
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by offbeat


Python3




# Python3 program to check
# if N is a hexagonal number
from math import sqrt
 
# Function to check
# if number is hexagonal
def isHexagonal(N):
     
    val = 8 * N + 1
    x = 1 + sqrt(val)
 
    # Calculate the value for n
    n = x / 4
 
    # Check if n - floor(n)
    # is equal to 0
    if ((n - int(n)) == 0):
        return True
    else:
        return False
 
# Driver code
if __name__ == '__main__':
     
    N = 14
     
    if (isHexagonal(N) == True):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by BhupendraSingh


C#




// C# program to check if
// N is a hexagonal number
using System;
 
class GFG{
 
// Function to check
// if number is hexagonal
static bool isHexagonal(int N)
{
    float val = 8 * N + 1;
    float x = 1 + (float)Math.Sqrt(val);
 
    // Calculate the value for n
    float n = (x) / 4;
 
    // Check if n - floor(n)
    // is equal to 0
    if ((n - (int)n) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int N = 14;
 
    if (isHexagonal(N) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by rutvik_56


Javascript




<script>
 
// Javascript program to check
// if N is a hexagonal number
 
// Function to check
// if number is hexagonal
function isHexagonal(N)
{
    let val = 8 * N + 1;
    let x = 1 + Math.sqrt(val);
     
    // Calculate the value for n
    let n = (x) / 4;
     
    // Check if n - floor(n)
    // is equal to 0
    if ((n - parseInt(n)) == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
// Driver code
let N = 14;
 
if (isHexagonal(N) == true)
{
    document.write("Yes" + "</br>");
}
else
{
    document.write("No");
}
 
// This code is contributed by Ankita saini
    
</script>


Output: 

No

 

Time Complexity: O(log(N)), for calculation square root.
Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32347 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS