Write a function in C ++ having 2 parameters x and n of integer type with result
type float to find the sum of following series :-
1 + x/2! + x2/ 3! +…………………..+xn/n+1!
1
Expert's answer
2013-03-11T10:58:07-0400
#include <iostream> #include <cmath>
using namespace std;
long factorial(int n) { long result = 1; while (n-- > 1) result *= n; return result; }
double series(int x, int n) { double result = 1; for (int i = 1; i < n; i++) result += pow(x, i) / factorial(i); return result; }
int main() { int x = 3; int n = 10; cout << "x = 3, n = 10" << endl; cout << "Series sum: " << series(x, n) << endl; return 0; }
Comments
Leave a comment