Skip to content
Related Articles

Related Articles

C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
  • Last Updated : 05 Dec, 2018

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 program to print the sum of series */
#include <bits/stdc++.h>
using namespace std;
  
/*function to calculate sum of given series*/
double sumOfSeries(double num)
{
    double res = 0, fact = 1;
    for (int i = 1; i <= num; i++) {
        /*fact variable store factorial of the i.*/
        fact = fact * i;
  
        res = res + (i / fact);
    }
    return (res);
}
  
/*Driver Function*/
int main()
{
    double n = 5;
    cout << "Sum: " << sumOfSeries(n);
    return 0;
}
Output:
Sum: 2.70833

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!

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :