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