C++ program to find n-th term in the series 7, 15, 32, …

C++Server Side ProgrammingProgramming

In this problem, we are given an integer N. The task is to find n-th term in series 7, 15, 32....

Let’s take an example to understand the problem,

Input

N = 6

Output

281

Explanation

The series upto nth term is 7, 15, 32, 67, 138, 281

Solution Approach

The solution to the problem lies in decoding the series. You can see the series is a mix of series.

The subtracting the values,

T(2) - T(1) = 15 - 7 = 8
T(3) - T(2) = 32 - 15 = 17

So, T(2) = 2*T(1) + 1
T(3) = 2*T(2) + 2

T(n) = 2*T(n-1) + (n-1)

So, the value of the nth term is found using the last term. To find these we will loop from 1 to n and find each value of the series.

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;
int findNthTerm(int n) {
if (n == 1)
return 7;
int termN = 7;
for (int i = 2; i <= n; i++)
termN = 2*termN + (i - 1);
return termN;
}
int main(){
int n = 12;
cout<<"The series is 7, 15, 32, 67...\n";
cout<<n<<"th term of the series is "<<findNthTerm(n);
return 0;
}

Output

The series is 7, 15, 32, 67...
12th term of the series is 18419
Published on 13-Mar-2021 12:15:49