Java Program to Find the Sum of Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N

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

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

Java Program to Print the Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N

On observing the pattern carefully, you can see in numerator first number is fixed as 1, second number is 2 added with the previous number while the exponent is fixed as 2.

In denominator, exponent of ‘a‘ starts from 1 to n.

Example:

Suppose,

a=2

n = 3

12 /2 + 32 /22  + 52 /23

= 1/2 + 9/4 + 25/8

= 0.5 + 2.25 + 3.13

= 5.88

Let’s see different ways to Java Program to Find the Sum of Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N.

Method-1: Java Program to Find the Sum of Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N By Using For Loop

Approach:

  • Declare an integer variable say ‘a’ which holds the value of a.
  • Declare an integer variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values of a and n.
  • Use a for loop from i=1 to i<=n
  • Inside for loop we will use another local variable ‘j’ which will be initiated from 1 and it will be incremented by 2 (for numerator)
  • Inside the loop we will first find pow(j,2) / Math.pow(a,i) and then add the value with ‘sum‘ in each 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 a ");
        int a = 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, j = 1; i <= n; i++, j=j+2)
              sum += Math.pow(j, 2) / Math.pow(a, i);
        System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
2
Enter the value of nth term n
3
Sum of the series is 5.875

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

Approach:

  • Declare an integer variable say ‘a’ which holds the value of a.
  • Declare an integer variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values of a and n.
  • Initialize an ineteger variable ‘i‘ and ‘j‘ as 1.
  • Use a while loop till i<=n
  • Inside while loop we will use another while loop and iterate it till j<=2*n
  • Inside the inner while loop we will first find pow(j,2) / Math.pow(a,i) and then add the value with ‘sum‘ in each iteration and Increment ‘j‘ by 2 and ‘i‘ by 1.
  • 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 a ");
        int a = 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;
        int j = 1;
       while(i <= n) 
        {
            while(j<= 2*n)
            {
                sum += Math.pow(j, 2) / Math.pow(a, i);
                j=j+2;
                i++;
            }
        }
        System.out.println("sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
2
Enter the value of nth term n
3
sum of the series is 5.875

Method-2: Java Program to Find the Sum of Series 1^2/a + 3^2/a^2 + 5^2/a^3 + …… + N By Using User Defined Method

Approach:

  • Declare an integer variable say ‘a’ which holds the value of a.
  • Declare an integer variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values of a and n.
  • Then call a user defined method by passing a and n as parameter.
  • Inside method use a for loop from i=1 to i<=n
  • Inside for loop we will use another local variable ‘j’ which will be initiated from 1 and it will be incremented by 2 (for numerator)
  • Inside the loop we will first find pow(j,2) / Math.pow(a,i) and then add the value with ‘sum‘ in each 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 a ");
        int a = 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(a,n);
   }
    public static void m1(int a, int n)
    {
        //for loop to print the series
        double sum = 0;
        for (int i = 1, j = 1; i <= n; i++, j=j+2)
            sum += Math.pow(j, 2) / Math.pow(a, i);
         System.out.println("Sum of the series is " + sum);
    }
}
Output:

Enter the value of a 
5
Enter the value of nth term n 
10
Sum of the series is 0.8749881344

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: