In the previous article, we have discussed about Java Program to Print the Series 1 12 123 1234 12345 N

In this article we are going to see how to find sum of series x – x^{3} + x^{5} – x^{7} + ……. + N by using java programming language.

## Java Program to Find the Sum of Series x – x^3 + x^5 – x^7 + …… + N

On observing the pattern carefully, we can see 1^{st} no is assigned positive and the 2^{nd} no is assigned negative sign and each term is taken as `x^i`

where value of ‘`i`

‘ starts from 1 and goes on with difference 2 like 1, 3, 5…

**Example:**

X = 2

N = 5

2 – 2^{3} + 2^{5} = 2-8+32 = 26

Let’s see different ways to find sum of series x – x^{3} + x^{5} – x^{7} + ……. + N.

### Method-1: Java Program to Find the Sum of Series x – x^3 + x^5 – x^7 + …… + N 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 exponent value of the series
- Create Scanner class object.
- Prompt the user to enter values for
`x`

and`n`

. - By using a 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(); //for loop to print the series int sum = 0; for (int i = 1, j = 1; i <= n; i = i + 2, j++) { if (j % 2 == 0) sum -= Math.pow(x, i); else sum += Math.pow(x, i); } System.out.println("Sum of the series is " + sum); } }

Output: Enter the value of x 2 Enter the value of last exponent 'n' 9 Sum of the series is 410

### Method-1: Java Program to Find the Sum of Series x – x^3 + x^5 – x^7 + …… + N 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 exponent value of the series
- Create Scanner class object.
- Prompt the user to enter values for
`x`

and`n`

. - By using a 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(); // while loop to print the series int sum = 0; int i = 1, j = 1; while(i <= n) { if (j % 2 == 0) sum -= Math.pow(x, i); else sum += Math.pow(x, i); i = i+2; j++; } System.out.println("Sum of the series is " + sum); } }

Output: Enter the value of x 2 Enter the value of last exponent 'n' 9 Sum of the series is 410

### Method-3: Java Program to Find the Sum of Series x – x^3 + x^5 – x^7 + …… + 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 values for
`x`

and`n`

. - Call a method to execute the sum series.
- By using a 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) { // for loop to print the series int sum = 0; for (int i = 1, j = 1; i <= n; i = i + 2, j++) { if (j % 2 == 0) sum -= Math.pow(x, i); else sum += Math.pow(x, i); } 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 is 1878005

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

**Try Yourself:**

- Write a program in c to find the sum of the series xx3 x5?
- Display the sum of the series 1 x x22 x33 in java?
- Write a program in python to find the sum of the series x x3 x5?
- x x2x3 x4 program in java?
- x x33 x55 x77 x99 in python?
- x – x^3/3 + x^5/5 – x^7/7 + x^9/9 in c++?
- Write a program in python to find the sum of the series x – x^3 + x^5?
- x-x^2+x^3-x^4 program in java?
- x – x^3/3 + x^5/5 – x^7/7 + x^9/9 in python?
- Write a program in c++ to find the sum of the series x+x^3 + x^5?
- Display the sum of the series 1 + x + x^2/2! + x^3/3! + … in java?
- Java program to find the sum of n natural numbers?
- Java program to find the sum of n numbers?
- Write a program in c to find the sum of the series x+x^3 + x^5?
- Write a program to find the sum of the series s=1+x+x2+x3+….xn?
- Write a program to find the sum of series in java?
- Java program to find sum of series 1+3+5+7?
- Java program to find the sum of three numbers?

**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!)