Program to find N-th term of series 2, 4, 3, 4, 15… in C++


In this problem, we are given a number N. Our task is to create a program to find N-th term of series 2, 4, 3, 4, 15… in C++.

Problem Description − To find the sum of the given series,

2, 4, 3, 4, 15, 0, 14, 16 .... N terms

We will find the formula for the general term of the series.

Let’s take an example to understand the problem,

Input − N = 9

Output − 9

Solution Approach:

The increase of the values in the series is linear i.e. no square values are in the series. Also, it’s value depends on other factors too (division by 2 and 3, as 6 gives 0).

So, we will first take out N (i.e. 1, 2, 3) from their value from the series.

Series: 1*(2), 2*(2), 3*(1), 4*(1), 5*(3), 6*(0), …

On observing this we can deduct the general formula is −

Tn = ( N*((N%2)+(N%3)) )

Program to show the implementation of our solution,

#include <iostream>
using namespace std;
int findNTerm(int N) {
int nthTerm = ( N*((N%2) + (N%3)) );
return nthTerm;
}
int main()
{
int N = 10;
cout<<N<<"th term of the series is "<<findNTerm(N);
return 0;
}

Output:

10th term of the series is 10

Updated on: 03-Oct-2020

109 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements