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

Nonagonal Numberis a figurate number that extends the concept of triangular and square numbers to the Nonagon. Specifically, thenthNonagonal Numbers count the number of dots in a pattern of n nested nonagons(9 sided polygon), all sharing a common corner, where theithnonagon in the pattern has sides made ofi dotsspaced one unit apart from each other. The first few Nonagonal Numbers are1, 9, 24, 46, 75, 111, 154, …

**Examples:**

Input:N = 9Output:YesExplanation:

Second Nonagonal Number is 9.

Input:N = 20Output:No

**Approach:**

1. The **K ^{th}** term of the nonagonal number is given as

2. As we have to check that the given number can be expressed as a Nonagonal Number or not. This can be checked as:

=>

=>

3. If the value of **K** calculated using the above formula is an integer, then **N** is a Nonagonal Number.

4. Else **N** is not a Nonagonal 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` `// is a Nonagonal Number` `bool` `isnonagonal(` `int` `N)` `{` ` ` `float` `n` ` ` `= (5 + ` `sqrt` `(56 * N + 25))` ` ` `/ 14;` ` ` `// Condition to check if the` ` ` `// number is a nonagonal number` ` ` `return` `(n - (` `int` `)n) == 0;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Number` ` ` `int` `N = 9;` ` ` `// Function call` ` ` `if` `(isnonagonal(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` `// nonagonal number` `public` `static` `boolean` `isnonagonal(` `int` `N)` `{` ` ` `double` `n = (` `5` `+ Math.sqrt(` `56` `* N + ` `25` `)) / ` `14` `;` ` ` ` ` `// Condition to check if the` ` ` `// number is a nonagonal number` ` ` `return` `(n - (` `int` `)n) == ` `0` `;` `}` `// Driver code ` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given number` ` ` `int` `N = ` `9` `;` ` ` ` ` `// Function call` ` ` `if` `(isnonagonal(N))` ` ` `{` ` ` `System.out.println(` `"Yes"` `);` ` ` `}` ` ` `else` ` ` `{` ` ` `System.out.println(` `"No"` `);` ` ` `}` `}` `}` `// This code is contributed by divyeshrabadiya07` |

## Python3

`# Python3 program for the above approach` `# Function to check if N is a` `# nonagonal number` `def` `isnonagonal(N):` ` ` `n ` `=` `(` `5` `+` `pow` `((` `56` `*` `N ` `+` `25` `), ` `1` `/` `2` `)) ` `/` `14` `;` ` ` `# Condition to check if the` ` ` `# number is a nonagonal number` ` ` `return` `(n ` `-` `int` `(n)) ` `=` `=` `0` `;` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `# Given number` ` ` `N ` `=` `9` `;` ` ` `# Function call` ` ` `if` `(isnonagonal(N)):` ` ` `print` `(` `"Yes"` `);` ` ` `else` `:` ` ` `print` `(` `"No"` `);` `# This code is contributed by Rajput-Ji` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to check if N is a` `// nonagonal number` `public` `static` `bool` `isnonagonal(` `int` `N)` `{` ` ` `double` `n = (5 + Math.Sqrt(56 * N + 25)) / 14;` ` ` ` ` `// Condition to check if the` ` ` `// number is a nonagonal number` ` ` `return` `(n - (` `int` `)n) == 0;` `}` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` ` ` `// Given number` ` ` `int` `N = 9;` ` ` ` ` `// Function call` ` ` `if` `(isnonagonal(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 N is a` `// is a Nonagonal Number` `function` `isnonagonal(N)` `{` ` ` `let n` ` ` `= (5 + Math.sqrt(56 * N + 25))` ` ` `/ 14;` ` ` `// Condition to check if the` ` ` `// number is a nonagonal number` ` ` `return` `(n - parseInt(n)) == 0;` `}` `// Driver Code` `// Given Number` `let N = 9;` `// Function call` `if` `(isnonagonal(N))` `{` ` ` `document.write(` `"Yes"` `);` `}` `else` `{` ` ` `document.write(` `"No"` `);` `}` `// This code is contributed by subhammahato348.` `</script>` |

**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 industry experts, please refer **Geeks Classes Live**