In the previous article, we have discussed about Java Program to Find the Sum of Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N

In this article we are going to see how to find sum of the series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n by using Java programming language.

## Java Program to Find the Sum of Series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n

On observing the pattern carefully, we can see Numerator is 1 (fixed)

In Denominator, the power/exponent of x is increasing by 1

**Example:**

x = 2 n = 3 1/2 + 1/2^2 + 1/2^3 = 1/2 + 1/4 + 1/8 = 0.5 + 0.25 + 0.125 = 0.875

Let’s see different ways to find sum of the series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n.

### Method-1: Java Program to Find the Sum of Series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n By Using For Loop

**Approach:**

- 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”. - Create Scanner class object.
- Prompt the user to enter value of
`x`

and`n`

. - Use a for loop
`from i=1 to i<=n`

- Inside loop we will first find the value of
`1/Math.pow(x,i)`

and then we will add the value in sum in every 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 value of nth term 'n' "); int n = s.nextInt(); //for loop to print the series double sum = 0; for (int i = 1; i <= n; i++) sum += 1 / Math.pow(x, i); System.out.println("Sum of the series is " + sum); } }

Output: Enter the value of x 2 Enter the value of nth term 'n' 3 Sum of the series is 0.875

### Method-2: Java Program to Find the Sum of Series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n By Using While Loop

**Approach:**

- 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”. - Create Scanner class object.
- Prompt the user to enter value of
`x`

and`n`

. - Use a while loop
`from i=1 to i<=n`

- Inside loop we will first find the value of
`1/Math.pow(x,i)`

and then we will add the value in sum in every 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 value of nth term 'n' "); int n = s.nextInt(); //while loop to print the series double sum = 0; int i = 1; while(i <= n) { sum += 1 / Math.pow(x, i); i ++; } System.out.println("sum of the series is " + sum); } }

Output: Enter the value of x 2 Enter the value of nth term 'n' 3 sum of the series is 0.875

### Method-3: Java Program to Find the Sum of Series 1/x + 1/x^2 + 1/x^3 + …… + 1/x^n By Using User Defined Method

**Approach:**

- 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”. - Create Scanner class object.
- Prompt the user to enter value of
`x`

and`n`

. - Call a method
`m1()`

by passing`x`

and`n`

as parameter. - Inside method use a for loop
`from i=1 to i<=n`

- Inside loop we will first find the value of
`1/Math.pow(x,i)`

and then we will add the value in sum in every 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 value of nth term '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 double sum = 0; for (int i = 1; i <= n; i++) sum += 1 / Math.pow(x, i); System.out.println("Sum of the series is " + sum); } }

Output: Enter the value of x 5 Enter the value of nth term 'n' 10 Sum of the series is 0.24999997440000005

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

**Related Java Programs:**

- Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + N
- Java Program to Find the Sum of Series 1 + (1/2!) + (1/3!) + (1/4!) + ……… + (1/n!)
- Java Program to Find the Sum of Series a + (a/2!) + (a/3!) + (a/4!) + …… + (a/n!)
- Java Program to Find the Sum of Series 1 + (3/2!) + (5/3!) + (7/4!) + …… + N