# Java Program to Print the Series x/1-x/2+x/3-x/4+x/5-…N

In the previous article we have discussed about Java Program to Print Series 4 16 36 64 100 …N

In this article we are going to see how to print the series x/1-x/2+x/3-x/4+x/5-……N  by using Java programming language, Series In Java, X = X ^ 3 In Java, Series In Java Program, Series Program In Java, Sum Of Series In Java include in this part.

## Java Program to Print the Series x/1-x/2+x/3-x/4+x/5-…N

On observing the pattern carefully, we can see

Numerator is x (fixed)

In Denominator, the number starts from 1 and the next number is 1 added with the previous denominator number.

The 1st term is positive and the 2nd term is negative and this goes on successively up to the nth term.

where, n is the number of terms in the series.

Example:

Suppose the value of x = 2 and n = 3
x/1 - x/2 + x/3
= 2/1 - 2/2 + 2/3
= 2-1+0.67
= 1.67

Let’s see different ways to print the series x/1-x/2+x/3-x/4+x/5-…N

### Method-1: Java Program to Print the Series x/1-x/2+x/3-x/4+x/5-…N By Using For Loop

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘x’, which holds the value of “x”
• Declare an integer variable say ‘n’, which holds the nth term of the series “n”
• Prompt the user to enter values for variable x and n
• Declare a double variable say ‘sum’ and assign the value to 0
• Use a for loop from i=1 to i<=n and incremented by 1
• Inside loop we will find the value of x / i, then we will check the if else condition to find the positive or negative term and then we will add that value in sum for each 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 x ");
int x = s.nextInt();
//Taking input of number of elements in the series
System.out.println("Enter the nth term 'n' ");
int n = s.nextInt();
//for loop to print the series
double sum = 0;
for (int i = 1; i <= n; i++)
{
double result =(double) x / i;
if (i % 2 == 0)
sum -= result;
else
sum += result;
}
System.out.println("Sum of the series is " +sum);
}
}

Output:

Enter the value of x
2
Enter the nth term 'n'
3
Sum of the series is 1.6666666666666665

### Method-2: Java Program to Print the Series x/1-x/2+x/3-x/4+x/5-…N By Using While Loop

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘x’, which holds the value of “x”
• Declare an integer variable say ‘n’, which holds the nth term of the series “n”
• Prompt the user to enter values for variable x and n
• Declare a double variable say ‘sum’ and assign the value to 0
• Declare an initialize an integer variable i=1
• Use a while loop till i<=n and incremented by 1
• Inside loop we will find the value of x / i, then we will check the if else condition to find the positive or negative term and then we will add that value in sum for each 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 x ");
int x = s.nextInt();
//Taking input of number of elements in the series
System.out.println("Enter the nth term 'n' ");
int n = s.nextInt();
//while loop to print the series
double sum = 0;
int i = 1;
while(i <= n)
{
double result =(double) x / i;
if (i % 2 == 0)
sum -= result;
else
sum += result;
i ++;
}
System.out.println("sum of the series is " + sum);
}
}

Output:

Enter the value of x
2
Enter the nth term 'n'
3
sum of the series is 1.6666666666666665

### Method-3: Java Program to Print the Series x/1-x/2+x/3-x/4+x/5-…N By Using User Defined Method

Approach:

• Create Scanner class object.
• Declare an integer variable say ‘x’, which holds the value of “x”
• Declare an integer variable say ‘n’, which holds the nth term of the series “n”
• Prompt the user to enter values for variable x and n
• Call a user defined method say printSeries() by passing x and n as parameter.
• Inside meclare a double variable say ‘sum’ and assign the value to 0
• Use a for loop from i=1 to i<=n and incremented by 1
• Inside loop we will find the value of x / i, then we will check the if else condition to find the positive or negative term and then we will add that value in sum for each 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 x ");
int x = s.nextInt();
//Taking input of number of elements in the series
System.out.println("Enter the nth term 'n' ");
int n = s.nextInt();
// calling m1 method to print the series
printSeries(x,n);
}
//user defined method printSeries() to print the series
public static void printSeries(int x, int n)
{
//for loop to print the series
double sum = 0;
for (int i = 1; i <= n; i++)
{
double result =(double) x / i;
if (i % 2 == 0)
sum -= result;
else
sum += result;
}
System.out.println("Sum of the series is " +sum);
}
}

Output:

Enter the value of x
5
Enter the nth term 'n'
7
Sum of the series is 3.797619047619048

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Check once:

• Write A Java Program To Print Value Of X^N.
• X-X^2+X^3-X^4 Program In Java
• 1+X+X^2+X^3+…+X^N Program In Java
• X-X^2+X^3-X^4 Program In C
• X+X^2+X^3+…+X^N Program In Java

Related Java Programs: