C++ program to find Nth term of series 1, 4, 15, 72, 420…

C++Server Side ProgrammingProgramming

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1, 4, 15, 72, 420…

Let’s take an example to understand the problem,

Input

N = 4

Output

72

Solution Approach

A simple approach to solve the problem is the formula for the Nth term of the series. For this, we need to observe the series and then generalise the Nth term.

The series can be seen as the product of factorial and a some variables,

1, 4, 15, 72, 420…
1!*(X1), 2!*(X2), 3!*(X3), 4!*(X4), 5!*(X5)...
1*(1), 2*(2), 6*(5/2), 24*(3), 120*(7/2)...

Here, the series of product is,

1, 2, 2.5, 3, 3.5…
It is {(n+2)/2}.

So the formula of Nth term is

T(N) = ( N! * (N + 2)/ 2 )

Program to illustrate the working of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
int calcFactorial(int N) {
   int factorial = 1;
   for (int i = 1; i <= N; i++)
      factorial = factorial * i;
      return factorial;
}
int calcNthTerm(int N) {
   return (calcFactorial(N) * (N + 2) / 2);
}
int main() {
   int N = 7;
   cout<<N<<"th term of the series is "<<calcNthTerm(N);
   return 0;
}

Output

7th term of the series is 22680
raja
Published on 13-Mar-2021 13:18:11
Advertisements