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));
}
}
Sum is: 18.0833