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

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

Enter the number of terms you want in the series as input. Now we use loops and factorials to calculate the sum and get the desired result.

Here is the source code of the Java Program to Find Sum of the Series 1/1! + 2/2! + 3/3! + …N/N!. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Sum_Series 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         double sum = 0;
  7.         int n;
  8.         System.out.println("1/1! + 2/2! + 3/3! + ..N/N!");
  9.         Scanner s = new Scanner(System.in);
  10.         System.out.print("Enter the no. of terms in series:");
  11.         n = s.nextInt();
  12.         Sum_Series obj = new Sum_Series();
  13.         for(int i = 1; i <= n; i++)
  14.         {
  15.             sum = sum + (double)i / (obj.fact(i));
  16.         }
  17.         System.out.println("Sum of series:"+sum);
  18.     }
  19.     int fact(int x)
  20.     {
  21.         int mul = 1;
  22.         while(x > 0)
  23.         {
  24.             mul = mul * x;
  25.             x--;
  26.         }
  27.         return mul;
  28.     }
  29. }

Output:

$ javac Sum_Series.java
$ java Sum_Series
 
1/1! + 2/2! + 3/3! + ..N/N!
Enter the no. of terms in series:5
Sum of series:2.708333333333333

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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.