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.

advertisement
  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 Reference Books in Java Programming, Data Structures and Algorithms.

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter