Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

Write a java program for a given 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

Below is the implementation of the above approach:

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)>


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!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 20 Oct, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials