Skip to content
Related Articles

Related Articles

Improve Article
Program to check if N is a Hendecagonal Number
  • Last Updated : 23 Mar, 2021

Given an integer N, the task is to check if N is a Hendecagonal Number or not. If the number N is an 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 hendecagonal number counts the number of dots in a pattern of n nested decagons, all sharing a common corner, where the ith hendecagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few hendecagonal numbers are 1, 11, 30, 58, 95, 141… 
 

Examples: 
 

Input: N = 11 
Output: Yes 
Explanation: 
Second hendecagonal number is 11.
Input: N = 40 
Output: No 
 

 



Approach: 
 

  1. The Kth term of the Hendecagonal Number is given as
    K^{th} Term = \frac{9*K^{2} - 7*K}{2}
     
  2. As we have to check that the given number can be expressed as a Hendecagonal Number or not. This can be checked as: 
     

=> N = \frac{9*K^{2} - 7*K}{2}
=> K = \frac{7 + \sqrt{72*N + 49}}{18}
 

  1.  
  2. If the value of K calculated using the above formula is an integer, then N is a Hendecagonal Number.
  3. Else N is not a Hendecagonal 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 if N is a
// Hendecagonal Number
bool ishendecagonal(int N)
{
    float n
        = (7 + sqrt(72 * N + 49))
          / 18;
 
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 11;
 
    // Function call
    if (ishendecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java




// Java program for the above approach
import java.lang.Math;
 
class GFG{
     
// Function to check if N is a
// hendecagonal number
public static boolean ishendecagonal(int N)
{
    double n = (7 + Math.sqrt(72 * N + 49)) / 18;
     
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code   
public static void main(String[] args)
{
         
    // Given number
    int N = 11;
     
    // Function call
    if (ishendecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07   

Python3




# Python3 program for the above approach
import math
 
# Function to check if N is a
# Hendecagonal Number
def ishendecagonal(N):
 
    n = (7 + math.sqrt(72 * N + 49))// 18;
 
    # Condition to check if the
    # number is a hendecagonal number
    return (n - int(n)) == 0;
 
# Driver Code
 
# Given Number
N = 11;
 
# Function call
if (ishendecagonal(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Nidhi_biet

C#




// C# program for the above approach
using System;
class GFG{
     
// Function to check if N is a
// hendecagonal number
public static bool ishendecagonal(int N)
{
    double n = (7 + Math.Sqrt(72 * N + 49)) / 18;
     
    // Condition to check if the
    // number is a hendecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code
public static void Main(string[] args)
{
         
    // Given number
    int N = 11;
     
    // Function call
    if (ishendecagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No\n");
    }
}
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
// javascript program for the above approach
 
 
// Function to check if N is a
// Hendecagonal Number
function ishendecagonal( N)
{
    let n
        = (7 + Math.sqrt(72 * N + 49))
          / 18;
 
    // Condition to check if the
    // number is a hendecagonal number
    return (n - parseInt(n)) == 0;
}
 
 
// Driver Code
 
    // Given Number
    let N = 11;
 
    // Function Call
    if (ishendecagonal(N)) {
        document.write( "Yes");
    }
    else {
        document.write( "No");
    }
 
// This code contributed by gauravrajput1
 
</script>
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
Recommended Articles
Page :