Heptagonal number

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 <iostream>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;} |
C
// C program to find the// nth Heptagonal number#include <stdio.h>// Function to return Nth Heptagonal// numberint heptagonalNumber(int n){ return ((5 * n * n) - (3 * n)) / 2;}// Drivers Codeint main(){ int n = 2; printf("%d\n",heptagonalNumber(n)); n = 15; printf("%d\n",heptagonalNumber(n)); return 0;}// This code is contributed by kothavvsaakash. |
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
<?php// PHP program to find the// nth Heptagonal number// Function to return Nth// Heptagonal numberfunction heptagonalNumber($n){ return ((5 * $n * $n) - (3 * $n)) / 2;}// Driver Code$n = 2;echo heptagonalNumber($n), "\n";$n = 15;echo heptagonalNumber($n);// This code is contributed // by anuj_67.?> |
Javascript
<script>// Javascript program to find the// nth Heptagonal number// Function to return Nth Heptagonal// numberfunction heptagonalNumber(n){ return parseInt(((5 * n * n) - (3 * n)) / 2);}// Drivers Codelet n = 2;document.write(heptagonalNumber(n) + "<br>");n = 15;document.write(heptagonalNumber(n) + "<br>");// This code is contributed by rishavmahato348.</script> |
Output :
7 540
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Reference: https://en.wikipedia.org/wiki/Heptagonal_number
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




