- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion

# Heptagonal number

A heptagonal number is a number which can be represented as a heptagon. A heptagon is a polygon with 7 sides. A heptagonal number can be represented as a combination of successive layers of heptagon( 7-sided polygon). Heptagonal number can be better explained with the below figures.

The first heptagonal number is 1. Thus, it can be represented by a single dot.

The second heptagonal number is 7 which can be represented by a heptagon.

The third heptagonal number is 18 which can be represented as a heptagon and combined with a successive layer of heptagon.

The fourth heptagonal number is 34. It can be represented as a heptagon combined with two successive heptagonal layers in the above shown manner which gives 34.

The similar concept will be used for further heptagonal numbers. With the same logic, the first few heptagonal numbers are **1, 7, 18, 34, 55, 81, 112, 148, 189, 235, 286, 342, 403**……

Our task in this problem includes that we will be given any positive number N as an input and we need to print the Nth Heptagonal number as our output.

For example,

**INPUT** : N=6

**OUTPUT** : 81

**INPUT** : N=9

**OUTPUT** : 189

Now let’s look at the algorithm that we will use to solve this problem.

## Algorithm

To solve this problem, we need to see the pattern that is being followed to calculate the nth heptagonal number. The nth heptagonal number can be represented as −

$$Heptagonal_{n}\:=\:\frac{n}{2}(5n\:-\:3)$$

If we closely observe the expression, every heptagonal number is of the form

$\frac{n}{2}(5n\:-\:3)$, where n represents the count of the heptagonal number.

Let's understand it better with examples.

For n=1,$\frac{1}{2}(5\:\times\:1\:-\:3)$= 1, which is the first heptagonal number.

For n=2,$\frac{2}{2}(5\:\times\:2\:-\:3)$= 7, which is the second heptagonal number.

For n=3,$\frac{3}{2}(5\:\times\:3\:-\:3)$= 18, which is the third heptagonal number.

Now, let's check it for n=8.$\frac{8}{2}(5\:\times\:8\:-\:3)$ gives 148 which is actually the eighth heptagonal number in the sequence of heptagonal numbers.

Since we can get any nth heptagonal number using the above expression, we will be using this expression to calculate the nth heptagonal number in our approach where n can be any positive number.

## Approach

The step-by-step illustration of the approach we will be following −

Take any positive number N as input to calculate the corresponding heptagonal number for the value N.

Initialise a function to calculate the Nth heptagonal number.

Use the expression mentioned in algorithm section i.e.$\frac{N}{2}(5N\:-\:3)$ to calculate the Nth heptagonal number and store it in any variable.

Return the variable in which we store the value which will be the Nth heptagonal number corresponding to any positive value N.

**Note** − We will use float data type instead of int data type to avoid any errors due to decimal values while calculating the Nth heptagonal number using the above mentioned formula.

## Example

The implementation of the approach in C++ −

#include <bits/stdc++.h> #include <iostream> using namespace std; //function to calculate nth heptagonal number using formula n/2(5n-3) float heptagonal(float N){ float ans= (N/2)*((5*N) - 3); //to store nth heptagonal number return ans; } int main(){ float N=5; //input float a=heptagonal(N); //store the answer in a variable N=13; float b=heptagonal(N); cout<<a<<endl<<b<<endl; //print the answer return 0; }

## Output

55 403

**Time Complexity : O(1) ,** since constant time is taken.

**Space Complexity : O(1) ,** since no extra space is taken.

## Conclusion

We have tried to learn the concept of heptagonal number and the formula used to calculate the nth heptagonal number which we used in our approach.

I hope you find this article helpful to learn the concept of printing nth heptagonal number for any user input for n.

- Related Articles
- What is the starting number of Natural number and Whole number ?
- Complete the following sentences:Every real number is either… number or… number.
- Which of the following are true for an element?Atomic number = number of protons + number of electronsMass number = number of protons + number of neutronsAtomic mass = number of protons = number of neutronsAtomic number = number of protons = number of electrons(a) (i) and (ii)(b) (i) and (iii)(c) (ii) and (iii)(d) (ii) and (iv)
- Is every rational number a real number?
- Finding a number, when multiplied with input number yields input number in JavaScript
- Convert octal number to decimal number in Java
- Count number of factors of a number - JavaScript
- Reynolds Number
- The isotopes of an element contain :(a) same number of neutrons but different number of protons(b) same number of neutrons but different number of electrons(c) different number of protons as well as different number of neutrons(d) different number of neutrons but same number of protons
- Which number is greater, the least $9$ digit number or the number which is $2$ more than the greatest $8$ digit number?
- Java program to convert decimal number to hexadecimal number
- C++ Program to Convert Octal Number to Binary Number
- Java Program to convert binary number to decimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert octal number to decimal number