Related Articles
Program to check if N is a Centered Octadecagonal number
• Last Updated : 07 Apr, 2021

Given an integer N, the task is to check if it is a Centered Octadecagonal number or not. Print “yes” if it is otherwise output is “no”.

Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first few Centered Octadecagonal numbers are 1, 19, 55, 109, 181, 271, 379, …

Examples:

Input: N = 19
Output: Yes
Explanation:
19 is the Second Centered Octadecagonal number is 19.

Input: 38
Output: No
Explanation:
38 is not a Centered Octadecagonal number.

Approach: To solve the problem mentioned above we know that the Kth term of the Centered Octadecagonal number is given as: As we have to check that the given number can be expressed as a Centered Octadecagonal number or not. This can be checked by generalizing the equation as:

=> => Finally, check the value of computation using this formula if it is an integer, then it means that N is a Centered Octadecagonal number.

Below is the implementation of the above approach:

## C++

 // C++ implementation to check that// a number is a Centered// Octadecagonal number or not #include using namespace std; // Function to check that the// number is a Centered// Octadecagonal numberbool isCenteredOctadecagonal(int N){     // Implement the formula generated    float n = (9 + sqrt(36 * N + 45)) / 18;     // Condition to check if the    // number is a Centered    // Octadecagonal number    return (n - (int)n) == 0;} // Driver Codeint main(){    int n = 19;     // Function call    if (isCenteredOctadecagonal(n)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}

## Java

 // Java implementation to check that// a number is a centered// octadecagonal number or notimport java.lang.Math; class GFG{     // Function to check that the// number is a centered// octadecagonal numberpublic static boolean isCenteredOctadecagonal(int N){         // Implement the formula generated    double n = (9 + Math.sqrt(36 * N + 45)) / 18;         // Condition to check if the    // number is a Centered    // Octadecagonal number    return(n - (int)n) == 0;} // Driver Code   public static void main(String[] args){    int n = 19;     // Function call    if (isCenteredOctadecagonal(n))    {        System.out.println("Yes");    }    else    {        System.out.println("No");    }}} // This code is contributed by divyeshrabadiya07

## Python3

 # Python3 implementation to check that# a number is a centered octadecagonal# number or notimport math # Function to check that the# number is a centered# octadecagonal numberdef isCenteredOctadecagonal(N):         # Implement the formula generated    n = (9 + math.sqrt(36 * N + 45)) / 18;         # Condition to check if the    # number is a centered    # octadecagonal number    return (n - int(n)) == 0 # Driver codeif __name__=='__main__':         n = 19         # Function call    if isCenteredOctadecagonal(n):        print('Yes')    else:        print('No') # This code is contributed by rutvik_56

## C#

 // C# implementation to check that// a number is a centered// octadecagonal number or notusing System; class GFG{ // Function to check that the// number is a centered// octadecagonal numberstatic bool isCenteredOctadecagonal(int N){     // Implement the formula generated    double n = (9 + Math.Sqrt(36 * N + 45)) / 18;         // Condition to check if the    // number is a Centered    // octadecagonal number    return (n - (int)n) == 0;}     // Driver Codestatic public void Main (){    int n = 19;         // Function call    if (isCenteredOctadecagonal(n))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} //This code is contributed by ShubhamCoder

## Javascript

 
Output:
Yes

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up