Program to find N-th term of series 0, 0, 2, 1, 4, 2, 6, 3, 8…in C++

C++Server Side ProgrammingProgramming

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

Problem description

To find the Nth term of the given series−

0, 0, 2, 1, 4, 2, 6, 3, 8 .... N Terms

We will find the general term of the series.

Let’s take an example to understand the problem,

Input

N = 8

Output

3

Solution Approach

To find the general term of the series, we need to closely observe the series. This series is a bit difficult to recognize as it is a mixture of two series. One series at even positions and one series at odd positions.

Odd Series− 0, 2, 4, 6, 8, 10, 12,....

Even Series− 0, 1, 2, 3, 4, 5, 6,....

Here,

If the nth term of the series is

Odd, the value is (n-1).

Even, the value is $T_{(n-1)}/2$

Example

 Live Demo

#include
using namespace std;
int findNTerm(int N) {

   if (N % 2 == 0)
      return findNTerm(N-1)/2;
   else {
      return (N-1);
   }
}
int main(){
   int N = 13;
   cout<<N<<"th term of the series is "<<findNTerm(N)<<endl;
}

Output

13th term of the series is 12
raja
Published on 04-May-2020 15:46:38
Advertisements