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

In the previous article, we have discussed about Java Program to Find the Sum of Series 1 + (1/2!) + (1/3!) + (1/4!) + ……… + (1/n!)

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

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

On observing the pattern carefully, we can see Numerator = a (fixed)

Denominator = starts from factorial of 1 to n

Example:

Suppose,
a = 2
n = 3
Then sum of series:
2 + (2/2!) + (2/3!) 
= 2 + 2/2 + 2/6 
= 2+1+0.33 
= 3.33

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

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of “a”
  • Declare an int variable say ‘n’ which holds the value of “n”
  • Create Scanner class object.
  • Prompt the user to enter values for a and 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 (a/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 a ");
        int a = s.nextInt();
        //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++) 
        {
            double fact = 1;
            for (int j = 1; j <= i; j++)
            {
                fact *= j;
            }
            sum += (a/fact);
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
2
Enter the value of n
3
Sum of the series is 3.3333333333333335

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of a
  • Declare an int variable say ‘n’ which holds the value of n
  • Create Scanner class object.
  • Prompt the user to enter values for a and n.
  • Initialize an integer variable ‘i‘ with 1.
  • Use a while loop till i<=n
  • Inside the loop we will again use another while loop to find the factorial and then we will find the value of (a/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 a ");
        int a = s.nextInt();
        //Taking input of number of elements in the series
        System.out.println("Enter the value of n ");
        int n = s.nextInt();
        // while loop to print the series
        double sum = 0.0;
        int i = 1;
        while(i <= n)
        {
            double fact = 1;
            int j = 1;
            while (j <= i)
            {
                fact *= j;
                j++;
            }
            sum += (a / fact);
            i++;
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
2
Enter the value of n
3
Sum of the series is 3.3333333333333335

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of “a”
  • Declare an int variable say ‘n’ which holds the value of “n”
  • Create Scanner class object.
  • Prompt the user to enter values for a and n.
  • Then call a user defined method by passing a and n as parameter.
  • Inside method use a for loop from i=1 to i<=n
  • And inside the loop we will again use another for loop to find the factorial and then we will find the value of (a/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 a ");
        int a = s.nextInt();
         //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(a,n);
   }
    public static void m1(int a, int n)
    {
        double sum = 0.0;
        for (int i = 1; i <= n; i++) 
        {
            double fact = 1;
            for (int j = 1; j <= i; j++)
            {
                fact *= j;
            }
            sum += (a/ fact);
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
5
Enter the value of last exponent n 
10
Sum of the series is 8.591409005731924

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: