In the previous article we have discussed about Java Program to Find the Sum of Series 1 + (1/2) + (1/3) + … + N

In this article we are going to see how to print sum of the series 1 + (1/3) + (1/5) + … + N by using Java programming language.

## Java Program to Find the Sum of Series 1 + (1/3) + (1/5) + … + N

On observing the pattern carefully, we can see each term is in `a/b`

format.

Numerator = 1 (Fixed)

Denominator = Odd number starting from 1

Like

1st term=1/1

2nd term=1/2

3rd term=1/3 like this.

And the series= 1/1+1/2+1/3 like this.

And our task is to first form the series and then find the sum of the series.

Let’s see different ways to print sum of the series 1 + (1/3) + (1/5) + … + N.

**Example:**

Suppose value of n = 3 1 + (1/2) + (1/3) = 1 + 0.5 + 0.34 = 1.84

### Method-1: Java Program to Find the Sum of Series 1 + (1/3) + (1/5) + … + N By Using For Loop

**Approach:**

- Declare an integer variable say ‘
`n`

’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
`n`

. - Declare an long variable say ‘
`sum`

‘ and initialize it to 0. - Declare an integer variable ‘
`count`

‘ and initialize it to 1. - Then declare integer variable
`m=n*n`

; - Use a for loop
`from i=1 to i<=m`

(incremented by 2) - Inside the for loop, find each term and add it to
`sum`

. Also keep track on count variable if`count==n`

then break the loop. - 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 'n' "); int n = s.nextInt (); double sum = 0; int count=1; int m = n * n; //for loop to print the series for (int i = 1; i <= m; i += 2) { System.out.println (1.0 / i); sum += (1.0 / i); if(count == n) break; count++; } System.out.println ("Sum of the series is " + sum); } }

Output: Enter the value of 'n' 3 1.0 0.3333333333333333 0.2 Sum of the series is 1.5333333333333332

### Method-2: Java Program to Find the Sum of Series 1 + (1/3) + (1/5) + … + N By Using While Loop

**Approach:**

- Declare an integer variable say ‘
`n`

’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
`n`

. - Declare an long variable say ‘
`sum`

‘ and initialize it to 0. - Declare an integer variable ‘
`count`

‘ and initialize it to 1. - Then declare integer variable
`m=n*n`

; - Declare an integer variable ‘
`i`

‘ and initialize it to 1. - Use a while loop till
`i<=m`

(incremented by 2) - Inside the while loop, find each term and add it to
`sum`

. Also keep track on count variable if`count==n`

then break the loop. - 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 'n' "); int n = s.nextInt (); double sum = 0; int count=1; int m = n * n; int i=1; //while loop to print the series while (i <= m) { System.out.println (1.0 / i); sum += (1.0 / i); if(count == n) break; count++; i+=2; } System.out.println ("Sum of the series is " + sum); } }

Output: Enter the value of 'n' 3 1.0 0.3333333333333333 0.2 Sum of the series is 1.5333333333333332

### Method-3: Java Program to Find the Sum of Series 1 + (1/3) + (1/5) + … + N By Using While Loop

**Approach:**

- Declare an integer variable say ‘
`n`

’ which holds the value of Nth term. - Create Scanner class object.
- Prompt the user to enter a number as value of
`n`

. - Then call a user defined method say
`printSeries()`

by passing`n`

as parameter. - Inside method declare an long variable say ‘
`sum`

‘ and initialize it to 0. - Declare an integer variable ‘
`count`

‘ and initialize it to 1. - Then declare integer variable
`m=n*n`

; - Use a for loop
`from i=1 to i<=m`

(incremented by 2) - Inside the for loop, find each term and add it to
`sum`

. Also keep track on count variable if`count==n`

then break the loop. - 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 'n' "); int n = s.nextInt (); //calling printSeries() method to print the series printSeries(n); } public static void printSeries(int n) { double sum = 0; int count=1; int m = n * n; int i=1; //for loop to print the series while (i <= m) { System.out.println (1.0 / i); sum += (1.0 / i); if(count == n) break; count++; i+=2; } System.out.println ("Sum of the series is " + sum); } }

Output: Enter the value of 'n' 3 1.0 0.3333333333333333 0.2 Sum of the series is 1.5333333333333332

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

**Related Java Programs:**