Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + N

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

In this article we are going to see how to print the sum of series x/2 + x/5 + x/8 + … + N  by using Java programming language.

Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + N

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

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

Example:

Suppose,
x = 2
n = 3

Then:
x/2 + x/5 + x/8  
= 2/2 + 2/5 + 2/8 
= 1+0.4 + 0.25 
= 1.65

Let’s see different ways to print the sum of series x/2 + x/5 + x/8 + … + N.

Method-1: Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + 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.
  • Create Scanner class object.
  • Prompt the user to enter values for x and n.
  • Use a for loop from i=2 to i<=n*n and incremented by 3.
  • Inside loop we will find the value of x/i and then we will add that value in sum in each iteration.
  • Then we will increment the count value, and then we will check if the count value is equal to n or not. If count is equal to n then we will break our 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 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;
       int count = 0;
       for (int i = 2; i <= n*n; i = i+3)
       {
            sum += (double)x / i;
           count++;
           if(count == n)
           break;
       }        
        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.65

Method-2: Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + 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.
  • Create Scanner class object.
  • Prompt the user to enter values for x and n.
  • Initialize integer variable ‘i‘ with value 2.
  • Use a while loop from till i<=n*n and incremented by 3.
  • Inside loop we will find the value of x/i and then we will add that value in sum in each iteration.
  • Then we will increment the count value, and then we will check if the count value is equal to n or not. If count is equal to n then we will break our 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 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 = 2;
        int count=0;
       while(i <= n*n) 
        {
            sum += (double)x / i;
           count ++;
           if(count == n)
            break;
            i +=3;
        }
        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.65

Method-3: Java Program to Find the Sum of Series x/2 + x/5 + x/8 + x/11 + …… + 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.
  • Create Scanner class object.
  • Prompt the user to enter values for x and n.
  • Call a user defined method say m1() and pass x and n as parameter.
  • Inside method use a for loop from i=2 to i<=n*n and incremented by 3.
  • Inside loop we will find the value of x/i and then we will add that value in sum in each iteration.
  • Then we will increment the count value, and then we will check if the count value is equal to n or not. If count is equal to n then we will break our 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 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
        m1(x,n);
   }
    public static void m1(int x, int n)
    {
        //for loop to print the series
        double sum = 0;
       int count = 0;
       for (int i = 2; i <= n*n; i = i+3)
       {
            sum += (double)x / i;
           count++;
           if(count == n)
            break;
       }
        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 5.480805958747134

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: