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

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

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

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

On observing the pattern carefully, we can see it is in the format of a/b

Numerator = 1 (fixed) in each term.

Denominator = Sum of n term means

in first term sum of 1

in second term sum of 1+2

in third term sum of 1+2+3

in nth term sum of 1+2+3+…+n

So the series for n terms looks like 1 + 1 / (1+2) + 1 / (1+2+3) + ……… + 1 / (1+2+3+ … + N).

Example:

Suppose n = 3
Then series:
1 + (1/1+2) + (1/1+2+3) 
= 1 + 1/3 + 1/6 
= 1 + 0.34 + 0.17 
= 1.51

Method-1: Java Program to Find the Sum of Series 1 + 1 / (1+2) + 1 / (1+2+3) + … + 1 / (1+2+3+ … + 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 double variable say ‘sum‘ and initialize it to 0.
  • Use a for loop from i=1 to i<=n (incremented by 1)
  • Declare an long variable say ‘term‘ and initialize it to 0.
  • Inside for loop, we will again do a for loop from j =1 to j<=i (incremented by 1)
  • Inside this loop we will get the sum of n term and store it in the variable “term”.
  • Now in the loop we will find the value of 1.0/ term and after that we will add it into sum for 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 'n' ");
        int n = s.nextInt();
        //for loop to print the series
        double sum = 0;
        for (int i = 1; i <= n; i++) 
        {
            long term = 0;
            for (int j = 1; j <= i; j++) 
            {
                term += j;
            }
            sum += (1.0 / term);
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

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

Method-1: Java Program to Find the Sum of Series 1 + 1 / (1+2) + 1 / (1+2+3) + … + 1 / (1+2+3+ … + 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 double variable say ‘sum‘ and initialize it to 0.
  • Continue a While loop till i<=n (from i=1 and loop incremented by 1)
  • Declare an long variable say ‘term‘ and initialize it to 0.
  • Inside while loop, we will again do a while loop till j<=i (from j=1 and loop incremented by 1)
  • Inside this loop we will get the sum of n term and store it in the variable “term”.
  • Now in the loop we will find the value of 1.0/ term and after that we will add it into sum for 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 'n' ");
        int n = s.nextInt();
        // while loop to print the series
       double sum = 0;
       int i = 1;
       while(i <= n)
       {  
    	    long term = 0;
            int j = 1;
            while ( j <= i) 
            {
                term += j;
                j++;
            }
            sum += (1.0 / term);
            i++;
       }
       System.out.println("Sum of the series is " + sum);
   }
}
Output:

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

Method-3: Java Program to Find the Sum of Series 1 + 1 / (1+2) + 1 / (1+2+3) + … + 1 / (1+2+3+ … + N) By Using User Defined 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.
  • Declare an double variable say ‘sum‘ and initialize it to 0.
  • Use a for loop from i=1 to i<=n (incremented by 1)
  • Declare an long variable say ‘term‘ and initialize it to 0.
  • Inside for loop, we will again do a for loop from j =1 to j<=i (incremented by 1)
  • Inside this loop we will get the sum of n term and store it in the variable “term”.
  • Now in the loop we will find the value of 1.0/ term and after that we will add it into sum for 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 'n' ");
        int n = s.nextInt();
       // calling printSeries() method to print the series
        printSeries(n);
   }
    public static void printSeries(int n)
    {
        double sum=0;
        for (int i = 1; i <= n; i++) 
        {
            long term = 0;
            for (int j = 1; j <= i; j++) 
            {
                term += j;
            }
            sum += (1.0 / term);
        }
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of 'n' 
4
Sum of the series is 1.6

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: