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

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

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

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

On observing the pattern carefully, we can see it is in the form of a/b!

In first term, numerator starts from 1 (fixed) and then each term is like 2 added with the previous number.

Denominator starts from 1! then in each term the factorial is incremented by 1 successively.

So the term will look like (1/1!) + (3/2!) + (5/3!) + (7/4!) + … to n, while ‘n‘ refers to last denominator value up to which series will continue.

Example:

Suppose n = 3
Then series =1 + (3/2!) + (5/3!) 
= 1 + 3/2 + 5/6 
= 1+1.5+0.83 
= 3.33

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

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

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Declare an double variable say ‘sum‘ and initialize it to 0.
  • Use a for loop from i =1 to i<=n (to continue up to nth term)
  • Inside for loop, we will get the numerator value from j=1 which will be incremented by j=j+2
  • Again inside that for loop, we will again use another for loop to find the factorial from k=1 to k<=i
  • Now in the loop we will find the value of j/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;
        for (int i = 1, j = 1; i <= n; i++, j = j + 2)
        {
            double fact = 1;
            for (int k = 1; k <= i; k++) 
            {
                fact *= k;
            }
            sum += j / fact;
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

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

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

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Declare an double variable say ‘sum‘ and initialize it to 0.
  • Use a while loop from till i<=n (i started from 1)
  • Inside the while loop, we will get the numerator value from j=1 which will be incremented by j=j+2
  • Again inside that while loop, we will again use another while loop to find the factorial from k=1 to k<=i
  • Now in the loop we will find the value of j/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();
        // while loop to print the series
       double sum = 0;
       int i = 1,j=1;
       while(i <= n)
       {
           double fact = 1;
           int k = 1;
           while( k <= i)
           {
               fact *= k;
               k++;
           }
           sum += j/ fact;
           j = j + 2;
           i++;
       }
       System.out.println("Sum of the series is " + sum);
   }
}
Output:

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

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

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Then call a user defined method say printSeries() by passing n as parameter.
  • Inside method declare an double variable say ‘sum‘ and initialize it to 0.
  • Use a for loop from i =1 to i<=n (to continue up to nth term)
  • Inside for loop, we will get the numerator value from j=1 which will be incremented by j=j+2
  • Again inside that for loop, we will again use another for loop to find the factorial from k=1 to k<=i
  • Now in the loop we will find the value of j/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();
       // calling m1 method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        double sum = 0;
        for (int i = 1, j = 1; i <= n; i++, j = j + 2)
        {
            double fact = 1;
            for (int k = 1; k <= i; k++) 
            {
                fact *= k;
            }
            sum += j / fact;
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of 'n' 
10
Sum of the series is 3.7182812500000004

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: