Java Program to Find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
Factorial of a number simply returns out the multiplication of that number with all numbers lesser than the number up to 1 over which factorial is applied. Now the question arises whether the series is convergent or divergent. According to concepts of Infinite series and factorial in mathematics, the series may not converge but it can contain a convergent subsequence.
n! = n * (n-1) × (n-2) × (n-3) × (n-4) × …… × 4 × 3 × 2 × 1
Input : n = 5 Processing : 1/1! + 2/2! + 3/3! + 4/4! + 5/5! 1 + 2/2 + 3/6 + 4/24 + 5/120 Output : 2.708333333333333
- Enter the number of terms N.
- Create a function to calculate the sum of series, say, calculateSum.
- In calculateSum function(), create a variable sum which stores the total sum of the series.
- Run a loop N times.
- Call factorial function() to calculate the factorial of a given number.
- Return sum.
The sum of series upto 5 terms is : 2.708333333333333
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.