Related Articles
Heptagonal number
• Last Updated : 07 Apr, 2021

Given a number n, the task is to find Nth heptagonal number. A Heptagonal number represents heptagon and belongs to a figurative number. Heptagonal has seven angles, seven vertices, and seven-sided polygon.
Examples :

Input : 2
Output :7
Input :15
Output :540

Few Heptagonal numbers are :
1, 7, 18, 34, 55, 81, 112, 148, 189, 235………..
A formula to calculate Nth Heptagonal number:

## C++

 // C++ program to find the// nth Heptagonal number#include using namespace std; // Function to return Nth Heptagonal// numberint heptagonalNumber(int n){    return ((5 * n * n) - (3 * n)) / 2;} // Drivers Codeint main(){     int n = 2;    cout << heptagonalNumber(n) << endl;    n = 15;    cout << heptagonalNumber(n) << endl;     return 0;}

## Java

 // Java program to find the// nth Heptagonal numberimport java.io.*; class GFG{// Function to return// Nth Heptagonal numberstatic int heptagonalNumber(int n){    return ((5 * n * n) - (3 * n)) / 2;} // Driver Codepublic static void main (String[] args){    int n = 2;    System.out.println(heptagonalNumber(n));    n = 15;    System.out.println(heptagonalNumber(n));}} // This code is contributed by anuj_67.

## Python3

 # Program to find nth# Heptagonal number # Function to find# nth Heptagonal numberdef heptagonalNumber(n) :         # Formula to calculate    # nth Heptagonal number    return ((5 * n * n) -            (3 * n)) // 2 # Driver Codeif __name__ == '__main__' :    n = 2    print(heptagonalNumber(n))    n = 15    print(heptagonalNumber(n))                 # This code is contributed# by ajit

## C#

 // C# program to find the// nth Heptagonal numberusing System; class GFG{// Function to return// Nth Heptagonal numberstatic int heptagonalNumber(int n){    return ((5 * n * n) -            (3 * n)) / 2;} // Driver Codepublic static void Main (){    int n = 2;    Console.WriteLine(heptagonalNumber(n));    n = 15;    Console.WriteLine(heptagonalNumber(n));}} // This code is contributed by anuj_67.

## PHP

 

## Javascript

 
Output :
7
540

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 DSA Live Classes

My Personal Notes arrow_drop_up