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

In the previous article, we have discussed about Java Program to Find the Sum of Series 2-4+6-8+…-/+N

In this article we are going to see how to print sum of the series 1 + 22 / a + 32 / a2 + …… to n terms by using Java programming language.

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

Example:

Suppose

a = 2

n = 3

1 + 22/2 + 33/22

= 1 + 4/2 + 27/4

= 1 + 2 + 6.85

= 9.75

Let’s see different ways to sum of the series 1 + 22 / a + 32 / a2 + …… to n.

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of a.
  • Declare an int variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values for a and n.
  • Declare an double variable say sum and initialize it with 1.
  • Use a for loop from i=2 to i<=n
  • Inside for loop we will find pow(i, i) / Math.pow(a, i-1) and then add each iteration with ‘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 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 = 1;
        for (int i = 2; i <= n; i++)
            sum += Math.pow(i, i) / Math.pow(a, i - 1);
        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 9.75

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of a.
  • Declare an int variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values for a and n.
  • Declare an double variable say sum and initialize it with 1.
  • Declare an integer variable say i and initialize it with 2.
  • Continue a while loop till i<=n
  • Inside while loop, find pow(i, i) / Math.pow(a, i-1) and then add each iteration with ‘sum‘. And increment 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 = 1;
        int i = 2;
        while(i <= n) 
        {
           sum += Math.pow(i, i) / Math.pow(a, i - 1);
            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 9.75

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

Approach:

  • Declare an int variable say ‘a’ which holds the value of a.
  • Declare an int variable say ‘n’ which holds the nth term of series
  • Create Scanner class object.
  • Prompt the user to enter values for a and n.
  • Then call a user defined method say m1() by passing a and n as parameter.
  • Inside method declare an double variable say sum and initialize it with 1.
  • Use a for loop from i=2 to i<=n
  • Inside for loop we will find pow(i, i) / Math.pow(a, i-1) and then add each iteration with ‘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 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 = 1;
        for (int i = 2; i <= n; i++)
            sum += Math.pow(i, i) / Math.pow(a, i - 1);
        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 6404.109488640001

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: