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

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 – x3 + x5 – x7 + ……. + 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 1st no is assigned positive and the 2nd 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 – 23 + 25 = 2-8+32 = 26

Let’s see different ways to find sum of series x – x3 + x5 – x7 + ……. + 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:

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

Related Java Programs: