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

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

Problem Description

Given a number n, write a Java Program to Find the Sum of the Series 1/1!+1/2!+1/3!+…1/N!.

Problem Solution


Start a loop from 1 to n, and add the factorial of the reciprocal of the loop variable to the sum.
The factorial of the loop variable can be calculated using a nested loop.

Program/Source Code

Here is the source code of the Java Program to Find the Sum of the series 1/1!+1/2!+1/3!+…1/N!. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Sum of the series 1/1!+1/2!+1/3!+...1/N!
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Series82 {
  8.     // Function to calculate and display the sum of the series.
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         int n;
  12.         try{
  13.             System.out.println("Enter the number of terms in the series");
  14.             n = Integer.parseInt(br.readLine());
  15.         }catch (Exception e){
  16.             System.out.println("An error occurred");
  17.             return;
  18.         }
  19.         double sum = 0;
  20.         double i;
  21.         int j;
  22.         long fac;
  23.         for(i=1; i<=n;i++){
  24.             fac =1;
  25.             for(j=2; j<=i;j++){
  26.                 fac*=j;
  27.             }
  28.             sum +=(1.0d/fac);
  29.         }
  30.         System.out.println("The sum is " + sum);
  31.     }
  32. }
Program Explanation

In the function main(), firstly the variable n is entered, then the loop for(i=1; i<=n;i++), is used to sum the reciprocal of the factorial of the loop variable. The loop for(j=2; j<=i;j++) is used to calculate the factorial of i.

advertisement
advertisement

Time Complexity: O(n2).

Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the number of terms in the series
34
The sum is 1.7182818284590455

Sanfoundry Global Education & Learning Series – Java Programs..

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.