Skip to content
Related Articles

Related Articles

Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 21 Jul, 2022

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

 

 

Java




// Java program to print the sum of series
 
import java.io.*;
import java.lang.*;
 
class GFG {
    public static 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);
    }
 
    public static void main(String[] args)
    {
        double n = 5;
        System.out.println("Sum: " + sumOfSeries(n));
    }
}
 
// Code contributed by Mohit Gupta_OMG <(0_o)>

Python3




#  program to print the sum of series
 
# importing math functions
from math import*
n=5
s=0
# iterating for loop up to n times
for i in range(1,n+1):
  # finding the sum series
  # factorial function will give you the
  # factorial of a number
  s=s+(i/factorial(i))
# printing the sum
print(s)
 
 
# this code is contributed by gangarajula laxmi

C#




// C# program to print the sum of series
using System;
 
class GFG {
    public static 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);
    }
 
    public static void Main(string[] args)
    {
        double n = 5;
        Console.WriteLine("Sum: " + sumOfSeries(n));
    }
}
 
// This code is contributed by phasing17

Output

Sum: 2.708333333333333

Time Complexity: O(n)

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!
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!