Program to find Star number in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a number n. Our task is to create a program to find Star number in C++.

Star Number is a special number that represents a centered hexagram (sixpoint star).

Some start numbers are 1, 13, 37, 73, 121.

Let’s take an example to understand the problem

Input

n = 5

Output

121

Solution Approach

To find the nth star number we will use the formula.

Let’s see the general formula for the star number.

n = 2 -> 13 = 12 + 1 = 6(2) + 1
n = 3 -> 37 = 36 + 1 = 6(6) + 1
n = 4 -> 73 = 72 + 1 = 6(12) + 1
n = 5 -> 121 = 120 + 1 = 6(20) + 1

For the above terms we can derive nth term.

Nth term = 6(n * (n-1)) + 1.

Validating it,

For n = 5, 6( 5 * 4) + 1 = 121

Program to illustrate the working our solution

Example

Live Demo

#include <iostream>
using namespace std;
int findStarNo(int n){
int starNo = ( 6*(n*(n - 1)) + 1 );
return starNo;
}
int main(){
int n = 4;
cout<<"The star number is "<<findStarNo(n);
return 0;
}

Output

The star number is 73
Published on 27-May-2020 09:33:49