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 tutorial, we will be discussing a program to find N-th term of series 0, 0, 2, 1, 4, 2, 6, 3, 8…

For this, we will be provided with a number. Our task is to find the term for the given series at that particular position.

Example

 Live Demo

#include <iostream>
#include <math.h>
using namespace std;
//finding nth term of given series
void findNthTerm(int n) {
   //if even
   if (n % 2 == 0) {
      n = n / 2;
      n = 2 * (n - 1);
      cout << n / 2 << endl;
   }
   //if odd
   else {
      n = (n / 2) + 1;
      n = 2 * (n - 1);
      cout << n << endl;
   }
}
int main() {
   int X = 10;
   findNthTerm(X);
   X = 7;
   findNthTerm(X);
   return 0;
}

Output

4
6
raja
Published on 04-May-2020 15:46:38
Advertisements