Related Articles

# Program to check if N is a Decagonal Number

• Last Updated : 23 Jun, 2021

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 numbers count the number of dots in a pattern of n nested decagons, all sharing a common corner, where the ith decagon in the pattern has sides made of i dots spaced one unit apart from each other. The first few decagonal numbers are 1, 10, 27, 52, 85, 126, 175, …

Examples:

Input: N = 10
Output: Yes
Explanation:
Second decagonal number is 10.
Input: N = 30
Output: No

Approach:

1. The Kth term of the decagonal number is given as 2. As we have to check that the given number can be expressed as a Decagonal Number or not. This can be checked as:

=> => 1.
2. If the value of K calculated using the above formula is an integer, then N is a Decagonal Number.
3. Else N is not a Decagonal Number.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to check if N is a// Decagonal Numberbool isdecagonal(int N){    float n        = (3 + sqrt(16 * N + 9))          / 8;     // Condition to check if the    // number is a decagonal number    return (n - (int)n) == 0;} // Driver Codeint main(){    // Given Number    int N = 10;     // Function call    if (isdecagonal(N)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}

## Java

 // Java program for the above approachimport java.lang.Math; class GFG{     // Function to check if N is a// decagonal numberpublic static boolean isdecagonal(int N){    double n = (3 + Math.sqrt(16 * N + 9)) / 8;         // Condition to check if the    // number is a decagonal number    return (n - (int)n) == 0;} // Driver code   public static void main(String[] args){             // Given number    int N = 10;         // Function call    if (isdecagonal(N))    {        System.out.println("Yes");    }    else    {        System.out.println("No");    }}} // This code is contributed by divyeshrabadiya07

## Python3

 # Python3 program for the above approachimport math # Function to check if N is a# decagonal numberdef isdecagonal(N):     n = (3 + math.sqrt(16 * N + 9)) / 8         # Condition to check if the    # number is a decagonal number    return (n - int(n)) == 0     # Driver Codeif __name__=='__main__':         # Given number    N = 10         # Function Call    if isdecagonal(N):        print('Yes')    else:        print('No') # This code is contributed by rutvik_56

## C#

 // C# program for the above approachusing System; class GFG{     // Function to check if N// is a decagonal Numberstatic bool isdecagonal(int N){    double n = (3 + Math.Sqrt(16 * N + 9)) / 8;         // Condition to check if the    // number is a decagonal number    return (n - (int)n) == 0;}     // Driver Codestatic public void Main (){         // Given Number    int N = 10;         // Function call    if (isdecagonal(N))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by ShubhamCoder

## Javascript

 
Output:
Yes

Time Complexity: O(1)

Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up