Program to check if N is a Dodecagonal Number
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 whether 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 <bits/stdc++.h>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
<script>// Javascript program for the above approach// Function to check if number N// is a dodecagonal number or notfunction isdodecagonal(N){ let n = (4 + Math.sqrt(20 * N + 16)) / 10; // Condition to check if the // N is a dodecagonal number return (n - parseInt(n)) == 0;}// Driver Code// Given Numberlet N = 12;// Function callif (isdodecagonal(N)){ document.write("Yes");}else{ document.write("No");}// This code is contributed by subhammahato348.</script> |
Yes
Time Complexity: O(log N), since sqrt() function has been used
Auxiliary Space: O(1)






Please Login to comment...