# Java Program to Find the Sum of Series 1 + (1/2!) + (1/3!) + (1/4!) + ……… + (1/n!)

In the previous article, we have discussed about Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + N

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

## Java Program to Find the Sum of Series 1 + (1/2!) + (1/3!) + (1/4!) + ……… + (1/n!)

On observing the pattern carefully, we can see

Numerator = 1 (fixed)

Denominator = starts from factorial 1 to n

Example:

N = 3

1 + (1/2!) + (1/3!) = 1 + 1/2 + 1/6 = 1+0.5+0.167 = 1.667

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

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

Approach:

• Declare an integer variable say ‘n’ which holds the value of “n
• 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 the loop we will again use another for loop to find the factorial and then we will find the value of 1/fact and after that we will add it into sum for every iteration.
• 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 'n' ");
int n = s.nextInt();
//for loop to print the series
double sum = 0.0;
for (int i = 1; i <= n; i++)
{
long fact = 1;
for (int j = 1; j <= i; j++)
{
fact *= j;
}
sum += (1.0 / fact);
}
System.out.println("Sum of the series is " + sum);
}
}

Output:

Enter the value of 'n'
3
Sum of the series is 1.6666666666666667

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

Approach:

• Declare an integer variable say ‘n’ which holds the value of “n
• Create Scanner class object.
• Prompt the user to enter a number as value of n.
• Use a while loop from i=1 to i<=n.
• Inside the loop we will again use another while loop to find the factorial and then we will find the value of 1/fact and after that we will add it into sum for every iteration.
• 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 'n' ");
int n = s.nextInt();
//for loop to print the series
double sum = 0.0;
int i=1;
while(i<= n)
{
long fact = 1;
int j=1;
while(j <= i)
{
fact *= j;
j++;
}
sum += (1.0 / fact);
i++;
}
System.out.println("Sum of the series is " + sum);
}
}
Output:

Enter the value of 'n'
4
Sum of the series is 1.7083333333333335

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

Approach:

• Create Scanner class object.
• Prompt the user to enter the value “n
• Call a method to execute the sum series
• Inside method use a for loop from i=1 to i<=n
• Inside the loop we will again use another for loop to find the factorial and then we will find the value of 0 / fact and after that we will add it into sum for every iteration.
• 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)
{
double sum = 0.0;
for (int i = 1; i <= n; i++)
{
long fact = 1;
for (int j = 1; j <= i; j++)
{
fact *= j;
}
sum += (1.0 / fact);
}
System.out.println("Sum of the series is " + sum);
}
}

Output:

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

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: