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

In the previous article, we have discussed about Java Program to Find the Sum of Series (x + (x^2/2!) + (x^ 3/3!) + …… + N term

In this article we are going to see how to find sum of the series (1/1!) + (2/2!) + …… + (N/N!)  by using java programming language.

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

On observing the pattern carefully, we can see clearly that each term is like the number itself divided by the factorial of that number while the number starts from 1 and goes up to N. For example if the number is ‘a‘ then the term is like a/a!

Example:

N = 5

(1/1!) + (2/2!) +(3/3!) + (4/4!) + (5/5!)
=> 1/1 + 2/2 + 3/6 + 4/24 + 5/120
=> 1 + 1 + 0.5 + 0.17 + 0.042 = 2.72

Let’s see different ways to find sum of the series (1/1!) + (2/2!) + …… + (N/N!).

Method-1: Java Program to Find the Sum of Series (1/1!) + (2/2!) + …… + (N/N!) By Using For Loop

Approach:

  • Declare an int variable say ‘n’ which holds the last term value of the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop from i=1 to i<=n
  • Inside loop find value of the term and keep track on sum of series.
  • Print the result

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of last exponent 'n' ");
        int n = s.nextInt();
        //for loop to print the series
       int fact=1;
        double sum=0;
       for(int i=1;i<=n;i++)
       {
        	fact*=i;
        	sum+=(double)i/fact;
       }
       System.out.println("Sum of the series is "+sum);
    }
}
Output:

Enter the value of last exponent 'n' 
5
Sum of the series is 2.708333333333333

Method-2: Java Program to Find the Sum of Series (1/1!) + (2/2!) + …… + (N/N!) By Using While Loop

Approach:

  • Declare an int variable say ‘n’  which holds the last term value of the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a while loop and iterate from i=1 till i<=n
  • Inside loop find value of the term and keep track on sum of series.
  • Print the result

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of last exponent 'n' ");
        int n = s.nextInt();
        int i=1,fact=1;
        double sum=0;
        //using while loop to print the series
        while ( i <= n )
        {
   	   fact*=i;
           sum+=(double)i/fact;
     	   i++;
        }
        System.out.println("Sum of the series is "+sum);
    }
}
Output:

Enter the value of last exponent 'n' 
5
Sum of the series is 2.708333333333333

Method-3: Java Program to Find the Sum of Series (1/1!) + (2/2!) + …… + (N/N!) By Using User Defined Method

Approach:

  • The same logic as method 1 but this time we are moving the for inside a user-defined method
  • Create Scanner class object.
  • Prompt the user to enter a number “x
  • Prompt the user to enter the last term value “n
  • Call a method to execute the sum series
  • Use a for loop i<=n
  • Inside loop find value of the term and keep track on sum of series.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
   {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of last exponent 'n' ");
        int n = s.nextInt();
        // calling m1 method to print the series
        m1(n);
   }
    public static void m1(int n)
    {
       //for loop to print the series
       double sum = 0;
       int fact=1;    
       for(int i=1;i<=n;i++)
       {
        	fact*=i;
        	sum+=(double)i/fact;
       }
       System.out.println("Sum of the series is "+sum);
    }
}
Output:

Enter the value of last exponent 'n' 
10
Sum of the series is 2.7182815255731922

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: