Find the nth term of the given series 0, 0, 2, 1, 4, 2, 6, 3, 8, 4… in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an integer value N. Our task is to Find the nth term of the given series −

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8, 18, 9, 20, 10… 

Let’s take an example to understand the problem,

Input − N = 6
Output − 2

Solution Approach

To find the Nth term of the series, we need to closely observe the series. It is the mixture of two series and odd and even terms of the series. Let’s see each of them,

At even positions −

  • T(2) = 0
  • T(4) = 1
  • T(6) = 2
  • T(8) = 3
  • T(10) = 4

The value at T(n) if n is even is {(n/2) - 1}

At Odd positions −

  • T(1) = 0
  • T(3) = 2
  • T(5) = 4
  • T(7) = 6
  • T(9) = 4

The value at T(n) if n is even is {n - 1}

Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;
bool isEven(int n){
   if(n % 2 == 0)
      return true;
   return false;
}
int findNthTerm(int n){
if (isEven(n))
      return ((n/ 2) - 1);
   else
      return (n - 1);
}
int main(){
   int N = 45;
   cout<<N<<"th term of the series is "<<findNthTerm(N);
   return 0;
}

Output

45th term of the series is 44
raja
Updated on 24-Jan-2022 08:37:36

Advertisements