Related Articles

# Program to check if N is a Dodecagonal Number

• Last Updated : 01 Apr, 2021

Given a number N, the task is to check if N is a Dodecagonal Number or not. If the number N is a Dodecagonal Number then print “Yes” else print “No”.

dodecagonal number represent Dodecagonal(12 sides polygon).The first few dodecagonal numbers are 1, 12, 33, 64, 105, 156, 217…

Examples:

Input: N = 12
Output: Yes
Explanation:
Second dodecagonal number is 12.

Input: N = 30
Output: No

Approach:

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

=> => 3. If the value of K calculated using the above formula is an integer, then N is a Dodecagonal Number.

4. Else the number N is not a Dodecagonal Number.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach#include using namespace std; // Function to check if number N// is a dodecagonal number or notbool isdodecagonal(int N){    float n        = (4 + sqrt(20 * N + 16))          / 10;     // Condition to check if the    // N is a dodecagonal number    return (n - (int)n) == 0;} // Driver Codeint main(){    // Given Number    int N = 12;     // Function call    if (isdodecagonal(N)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{ // Function to check if number N// is a dodecagonal number or notstatic boolean isdodecagonal(int N){    float n = (float) ((4 + Math.sqrt(20 * N +                                      16)) / 10);     // Condition to check if the    // N is a dodecagonal number    return (n - (int)n) == 0;} // Driver Codepublic static void main(String[] args){         // Given Number    int N = 12;     // Function call    if (isdodecagonal(N))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by sapnasingh4991

## Python3

 # Python3 program for the above approachimport numpy as np # Function to check if number N# is a dodecagonal number or notdef isdodecagonal(N):     n = (4 + np.sqrt(20 * N + 16)) / 10     # Condition to check if the    # N is a dodecagonal number    return (n - int(n)) == 0 # Driver CodeN = 12 # Function callif (isdodecagonal(N)):    print("Yes")else:    print("No") # This code is contributed by PratikBasu

## C#

 // C# program for the above approachusing System; class GFG{ // Function to check if number N// is a dodecagonal number or notstatic bool isdodecagonal(int N){         float n = (float) ((4 + Math.Sqrt(20 * N +                                      16)) / 10);     // Condition to check if the    // N is a dodecagonal number    return (n - (int)n) == 0;} // Driver Codepublic static void Main(string[] args){         // Given number    int N = 12;     // Function call    if (isdodecagonal(N))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by rutvik_56

## 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