C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
You have been given a series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!, find out the sum of the series till nth term. Examples:
Input :n = 5
Output : 2.70833
Input :n = 7
Output : 2.71806
CPP
#include <bits/stdc++.h>
using namespace std;
double sumOfSeries( double num)
{
double res = 0, fact = 1;
for ( int i = 1; i <= num; i++) {
fact = fact * i;
res = res + (i / fact);
}
return (res);
}
int main()
{
double n = 5;
cout << "Sum: " << sumOfSeries(n);
return 0;
}
|
Time Complexity : O(n), where n is the number
Auxiliary Space : O(1)
Please refer complete article on Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n! for more details!
Last Updated :
17 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...