// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!
#include
using namespace std;
// An Efficient Function to return value of 1/1! + 1/2! + .. + 1/n!
double sum(int n)
{
double sum = 0;
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact *= i; // Update factorial
sum += 1.0/fact; // Update series sum
}
return sum;
}
// Driver program to test above functions
int main()
{
int n = 5;
cout << sum(n);
return 0;
}