Related Articles

# Java Program to Find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!

• Last Updated : 18 Aug, 2021

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.

Illustration:

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

Approach :

• 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.

Implementation:

## Java

 `// Java Program to Find Sum of the Series``// 1/1! + 2/2! + 3/3! + ……1/N!` `// Importing java generic libraries``import` `java.io.*;` `class` `GFG {``  ` `    ``// Function(recursive) to calculate factorial``    ``public` `static` `double` `factorial(``int` `i)``    ``{``        ``// Step1: Base case``        ``if` `(i == ``1``) {``            ``return` `1``;``        ``}``        ``// Step2&3: Recursion execution & call statements``        ``return` `i * factorial(i - ``1``);``    ``}` `    ``// Fuction to calculate sum of series``    ``public` `static` `double` `calculateSum(``int` `N)``    ``{``        ``// Store total_sum in sum``        ``double` `sum = ``0``;` `        ``// Iteration by running a loop N times``        ``for` `(``int` `i = ``1``; i <= N; i++) {``            ``sum = sum + ((``double``)i / factorial(i));``        ``}` `        ``// Return calculated final sum``        ``return` `sum;``    ``}` `    ``// Main driver method``    ``public` `static` `void` `main(String[] args)``    ``{``        ``/* No of terms in series``           ``taken inn order to show output */``        ``int` `N = ``5``;` `        ``// Print sum of series by``        ``// calling function calculating sum of series``        ``System.out.println(``"The sum of series upto "` `+ N``                           ``+ ``" terms is : "``                           ``+ calculateSum(N));``    ``}``}`
Output
`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.

My Personal Notes arrow_drop_up