Program to find N-th term of series 3, 5, 33, 35, 53… in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find N-th term of series 3, 5, 33, 35, 53…

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 <bits/stdc++.h>
using namespace std;
//finding the nth term in the series
int printNthElement(int n){
   int arr[n + 1];
   arr[1] = 3;
   arr[2] = 5;
   for (int i = 3; i <= n; i++) {
      if (i % 2 != 0)
         arr[i] = arr[i / 2] * 10 + 3;
      else
         arr[i] = arr[(i / 2) - 1] * 10 + 5;
   }
   return arr[n];
}
int main(){
   int n = 6;
   cout << printNthElement(n);
   return 0;
}

Output

55
raja
Published on 04-May-2020 10:47:59
Advertisements