Java Program to Find the Sum of Series (x + (x^2/2!) + (x^3/3!) + …… + N terms

In the previous article, we have discussed about Java Program to Find the Sum of Series (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + …… + N

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

Java Program to Find the Sum of Series (x + (x^2/2!) + (x^ 3/3!) + …… + N terms

Let’s understand the series with an example.

Example:

Suppose the value of 
x = 2
N = 5
Then the series:
2 + (2^2 / 2!) + (2^3 / 3!) + (2^4 / 4!) +  (2^5/ 5!)
=> 2+ 4/2 + 8/6 + 16/24 + 32/120  =  2 + 2 + 1.34 + 0.67 + 0.27 = 6.28

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

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

Approach:

  • Declare an int variable say ‘x’ which holds the number of whose we will be calculating the sum of the series.
  • Declare an int variable say ‘n’ which holds the last term value of the series.
  • Create Scanner class object.
  • Prompt the user to enter values for x and n.
  • Use a for loop from i=1 to i<=n.
  • Inside loop find value of each 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 x ");
        int x = 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();
        //for loop to print the series
        int fact=1;
        double sum=0;
        for(int i=1;i<=n;i++)
       {
       	    double a=Math.pow(x,i);
            fact*=i;
            sum+=a/fact;
        }
        System.out.println("Sum of the series is "+sum);
    }
}
Output:

Enter the value of x 
2
Enter the value of last exponent 'n' 
5
Sum of the series is 6.266666666666667

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

Approach:

  • Declare an int variable say ‘x’ which holds the number of whose we will be calculating the sum of the series.
  • Declare an int variable say ‘n’ which holds the last term value of the series
  • Create Scanner class object.
  • Prompt the user to enter values for x and n.
  • Use a while loop from i=1 to i<=n
  • Inside loop find value of each 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 x ");
        int x = 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();
        //while loop to print the series
        int fact=1;
      double sum=0;
      int i=1;
        while(i<=n)
       {
       	    double a=Math.pow(x,i);
            fact*=i;
            sum+=a/fact;
            i++;
        }
        System.out.println("Sum of the series is"+sum);
    }
}
Output:

Enter the value of x 
2
Enter the value of last exponent 'n' 
5
Sum of the series is6.266666666666667

Method-1: Java Program to Find the Sum of Series (x + (x^2/2!) + (x^ 3/3!) + …… + N terms 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 values for x and n.
  • Call a method to execute the sum series by passing values of x and n as parameter.
  • Use a for loop from i=1 to i<=n
  • Inside loop find value of each 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 x ");
        int x = 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(x,n);
   }
    public static void m1(int x, int n)
    {
        //for loop to print the series
        int fact=1;
        double sum=0;
        for(int i=1;i<=n;i++)
       {
       	    double a=Math.pow(x,i);
            fact*=i;
            sum+=a/fact;
        }
        System.out.println("Sum of the series is"+sum);
    }
}
Output:

Enter the value of x 
5
Enter the value of last exponent 'n' 
10
Sum of the series is145.38060102513225

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: