Question #41309

computes the sum of the following series.

Sum=1!+2!+3!+4!+........n!

Sum=1!+2!+3!+4!+........n!

Expert's answer

`#include <iostream>`

long factorial(long n) {

if ( n < 0 ) {

return -1;

}

if ( n < 2 ) {

return 1;

} else {

return n * factorial(n-1);

}

}

int main() {

int fact;

long long unsigned sum = 0;

std::cout << "Please enter N " << std::endl;

std::cin >> fact;

for ( long i = 1; i <= fact; i++) {

sum += factorial(i);

}

std::cout << "The sum is " << sum << std::endl;

return 0;

}

## Comments

## Leave a comment