Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)!

For this we will be provided with the value of x and n. Our task is to calculate the values accordingly in the given series and find its sum.

Example

 Live Demo

#include <iostream>
#include <math.h>
using namespace std;
//finding factorial of a number
int fact(int n) {
   if (n == 1)
   return 1;
   return n * fact(n - 1);
}
//calculating the sum
double sum(int x, int n) {
   double i, total = 1.0;
   for (i = 1; i <= n; i++) {
      total = total + (pow(x, i) / fact(i + 1));
   }
   return total;
}
int main() {
   int x = 5, n = 4;
   cout << "Sum is: " << sum(x, n);
   return 0;
}

Output

Sum is: 18.0833
raja
Published on 27-May-2020 13:04:55
Advertisements