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

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

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

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

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

Numerator = Starts from 1 and in each term incremented by 1

Denominator = In each term Numerator+1

Like

1st term=1/2

2nd term=2/3

3rd term=3/4 like this.

And the series= 1/2+2/3+3/4 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/2) + (2/3) + (3/4) + …… + N.

Example:

Suppose value of n = 3

(1/2) + (2/3) + (3/4) 
= 0.5+0.66+0.75 
= 1.91

Method-1: Java Program to Find the Sum of Series (1/2) + (2/3) + (3/4) + …… + 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.
  • Use a for loop from i=1 to i<=100 (incremented by 1)
  • Inside the for loop, find each term and add it to sum.
  • 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;
        //for loop to print the series
        for (int i = 1; i <= n; i++)
        {   
            sum += ( i/(double)(i+1));
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of 'n' 
3
Sum of the series is 1.9166666666666665

Method-2: Java Program to Find the Sum of Series (1/2) + (2/3) + (3/4) + …… + 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.
  • Use a for loop from i=1 to i<=100 (incremented by 1)
  • Inside the for loop, find each term and add it to sum.
  • 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 i=1;
        //while loop to print the series
        while (i <= n)
        {   
            sum += ( i/(double)(i+1));
            i++;
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Output: Enter the value of 'n' 
3
Sum of the series is 1.9166666666666665

Method-3: Java Program to Find the Sum of Series (1/2) + (2/3) + (3/4) + …… + 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.
  • Use a for loop from i=1 to i<=100 (incremented by 1)
  • Inside the for loop, find each term and add it to sum.
  • 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;
        //for loop to print the series
        for (int i = 1; i <= n; i++)
        {
            sum += ( i/(double)(i+1));
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of 'n' 
5
Sum of the series is 3.5500000000000003

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: