Question #32317

Write a function seqsum() in c++ with two arguments, double x and int n. The function should return a value of type double and it should find the sum of the following series:
1+ x/(2!) + x^2/(4!) +........+ x^n/((2n)!)

Expert's answer

#include <iostream>

#include <cmath>

using namespace std;

long factorial(int n)

{

long result = 1;

while (n)

result *= n--;

return result;

}

double seqsum(double x, int n)

{

double result = 1.0;

for (int k = 1; k <= n; k++)

result += pow(x, k) / factorial(2 * k);

return result;

}

int main()

{

cout << "x = 1:" << endl;

for (int n = 0; n < 5; n++)

cout << " n = " << n << " " << seqsum(1, n) << endl;

return 0;

}

#include <cmath>

using namespace std;

long factorial(int n)

{

long result = 1;

while (n)

result *= n--;

return result;

}

double seqsum(double x, int n)

{

double result = 1.0;

for (int k = 1; k <= n; k++)

result += pow(x, k) / factorial(2 * k);

return result;

}

int main()

{

cout << "x = 1:" << endl;

for (int n = 0; n < 5; n++)

cout << " n = " << n << " " << seqsum(1, n) << endl;

return 0;

}

## Comments

## Leave a comment