C++ program to find n-th term of series 1, 4, 27, 16, 125, 36, 343...

C++Server Side ProgrammingProgramming

In this problem, we are given an integer N. The task is to find the n-th term in series 1, 4, 27, 16, 125, 36, 343....

Let’s take an example to understand the problem,

Input

N = 7

Output

343

Explanation

The series is 1,4, 27, 16, 125, 36, 343…

Solution Approach

A simple solution to the problem is by finding the general term of the series. This series comprises two different series one at odd terms and one at even terms. If the current element index is even, the element is square of its index. And if the current element index is odd, the element is the cube of its index.

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int findNthTerm(int N) {
   if (N % 2 == 0)
      return (N*N);
   return (N*N*N);
}
int main() {
   int N = 8;
   cout<<"The "<<N<<"th term of the series is "<<findNthTerm(N);
   return 0;
}

Output

The 8th term of the series is 64
raja
Published on 13-Mar-2021 12:19:38
Advertisements