In the previous article, we have discussed about Java Program to Print the Series x – x^3 + x^5 – x^7 + …… + N

In this article we are going to see how to find sum of the series 1 – x^{2}/2! + x4/4! – x^{6}/6! + ……. x^{n}/n! by using Java programming language.

## Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/n!

On observing the pattern carefully, we can see

- 1
^{st}no. Is always starts with 1 - 1
^{st}no is assigned positive and the 2^{nd}no is assigned negative sign.

**Example:**

X = 2

N = 5

1 – 2^{2}/2! + 2^{4}/4! = 1-2+2/3 = -0.3333

Let’s see different ways to find sum of the series 1 – x^{2}/2! + x4/4! – x^{6}/6! + ……. x^{n}/n!

### Method-1: Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/n! By Using 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`

. - Check if n is even or not, if not even then update it to nearest even no.
- By using for loop find 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(); int a = 1; double sum = 0; // checking if n is even or not if (n % 2 != 0) { n = n-1; } //for loop to print the series for (int i = 0; i <= n; i += 2) { long b = 1; // to find factorial for (int j = 1; j <= i; j++) { b *= j; } double t = Math.pow(x, i) / b * a; sum += t; a *= -1; } System.out.println("Sum = " + sum); } }

Output: Enter the value of x 2 Enter the value of last exponent 'n' 5 Sum = -0.33333333333333337

### Method-2: Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/n! By Using While Loop

**Approach:**

- Declare an int variable say ‘x’ which holds the number of which 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`

. - Check if n is even or not, if not even then update it to nearest even no.
- By using while loop find 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(); int a = 1; double sum = 0; // checking if n is even or not if (n % 2 != 0) { n = n-1; } //for loop to print the series int i = 0; while(i <= n) { long b = 1; int j = 1; // to find factorial while(j <= i) { b *= j; j++; } double t = Math.pow(x, i) / b * a; sum += t; a *= -1; i += 2; } System.out.println("Sum = " + sum); } }

Output: Enter the value of x 2 Enter the value of last exponent 'n' 5 Sum = -0.33333333333333337

### Method-3: Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/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 as value of “x”
- Prompt the user to enter a number as value of “n”
- Call a method to execute the sum series.
- By using for loop find 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) { // initial no. Is 1 int a = 1; double sum = 0; // checking if n is even or not if (n % 2 != 0) { n = n-1; } //for loop to print the series for (int i = 0; i <= n; i += 2) { long b = 1; // to find factorial for (int j = 1; j <= i; j++) { b *= j; } double t = Math.pow(x, i) / b * a; sum += t; a *= -1; } System.out.println("Sum = " + sum); } }

Output: Enter the value of x 5 Enter the value of last exponent 'n' 10 Sum = -0.16274663800705413

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

**Related Java Programs:**

- Java Program to Find the Sum of Series 1 – x2/2! + x4/4! – x6/6! + …… xn/n
- Java Program to Find the Sum of Series (x2 / 1!) + (x4 / 3!) + (x6 / 5!) + …… + N
- Java Program to Find the Sum of Series (x + (x^2/2!) + (x^ 3/3!) + …… + N terms
- Java Program to Find the Sum of Series (1/1!) + (2/2!) + …… + (N/N!)