Related Articles

# Program to check if N is a Heptagonal Number

• Last Updated : 13 Apr, 2021

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

Heptagonal Number represents Heptagon and belongs to a figurative number. Heptagonal has seven angles, seven vertices, and seven-sided polygon. The first few Heptagonal Numbers are 1, 7, 18, 34, 55, 81, …

Examples:

Input: N = 7
Output: Yes
Explanation:
Second heptagonal number is 7.
Input: N = 30
Output: No

Approach:

1. The Kth term of the heptagonal number is given as 2. As we have to check that the given number can be expressed as a Heptagonal 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 Heptagonal Number.
3. Else N is not a Heptagonal 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// Heptagonal numberbool isheptagonal(int N){    float n        = (3 + sqrt(40 * N + 9))          / 10;     // Condition to check if the    // number is a heptagonal number    return (n - (int)n) == 0;} // Driver Codeint main(){    // Given Nuber    int N = 7;     // Function call    if (isheptagonal(N)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}

## Java

 // Java program for the above approachimport java.io.*;import java.util.*; class GFG {     // Function to check if N// is a heptagonal numberpublic static boolean isheptagonal(int N){    double n = (3 + Math.sqrt(40 * N + 9)) / 10;         // Condition to check if the number    // is a heptagonal number    return (n - (int)n) == 0;} // Driver codepublic static void main(String[] args){         // Given Number    int N = 7;             // Function call    if (isheptagonal(N))    {        System.out.println("Yes");    }    else    {        System.out.println("No");    }}} // This code is contributed by coder001

## Python3

 # Python3 program for the above approachimport math # Function to check if N is a# heptagonal numberdef isheptagonal(N):         n = (3 + math.sqrt(40 * N + 9)) / 10         # Condition to check if the    # number is a heptagonal number    return (n - int(n)) == 0     # Driver CodeN = 7 # Function callif (isheptagonal(N)):    print("Yes")else:    print("No")     # This code is contributed by Shubham_Coder

## C#

 // C# program for the above approachusing System; class GFG {     // Function to check if N// is a heptagonal numberpublic static bool isheptagonal(int N){    double n = (3 + Math.Sqrt(40 * N + 9)) / 10;         // Condition to check if the number    // is a heptagonal number    return (n - (int)n) == 0;} // Driver codepublic static void Main(String[] args){         // Given Number    int N = 7;             // Function call    if (isheptagonal(N))    {        Console.WriteLine("Yes");    }    else    {        Console.WriteLine("No");    }}} // This code is contributed by Rohit_ranjan

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