n’th Pentagonal Number in C++

C++Server Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we are going to write a program that finds the n-th pentagonal number.

A pentagonal number is a number represented as dots or pebbles arranged in the shape of a regular polygon. Refer to the wiki for better understanding.

The n-th pentagonal number is (3 * n * n - n) / 2.

The series of pentagonal numbers are 1, 5, 12, 22, 35, 51, 70, 92...

Algorithm

• Initialise the number n.
• Use the formula to find the n'th pentagonal number.
• Print the resultant number.

Implementation

Following is the implementation of the above algorithm in C++

#include<bits/stdc++.h>

using namespace std;

int getNthPentagonalNumber(int n) {
return (3 * n * n - n) / 2;
}
int main() {
int n = 7;
cout << getNthPentagonalNumber(n) << endl;
return 0;
}

Output

If you run the above code, then you will get the following result.

70
Updated on 22-Oct-2021 11:04:32