Java Program to Find Sum of Series a^2 + a^2/2 + a^2/3 + …… + a^2/10

In the previous article, we have discussed about Java Program to Print Series 1 4 9 16 25 36 …N

In this article we are going to see how to print the sum of the series a2, a2 / 2, a2 / 3, ……, a2 / 10 using Java programming language.

Java Program to Find Sum of Series a^2 + a^2/2 + a^2/3 + …… + a^2/10

Here ‘a‘ is an integer value. First you have to find the value of each element of series and then sum of the series.

Let’s see different ways to print the sum of the series a2, a2 / 2, a2 / 3, ……, a2 / 10.

Method-1: Java Program to Find Sum of Series a^2 + a^2/2 + a^2/3 + …… + a^2/10 By Using Math.pow() Method

Approach:

  1. Create Scanner class object.
  2. Prompt the user to enter a number and store it in a variable a.
  3. Run a for loop from i=1 to a and print Math.pow(a,2)/i.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
       // take user input
        System.out.print("Enter the value of a: ");
        int a = sc.nextInt();
        // initialise sum
        double sum = 0;
        // print the sum using loop
        System.out.print("The sum of the series [");
        
        //for loop to find the series and it's sum
        for (int i = 1; i <= 10; i++) 
        {
            sum += Math.pow(a, 2) / i;
            System.out.print(Math.pow(a, 2)/i + ", ");
        }
        System.out.print("] is " + sum);
    }
}

Output:

Enter the value of a: 5
The sum of the series [25.0, 12.5, 8.333333333333334, 6.25, 5.0, 4.166666666666667, 3.5714285714285716, 3.125, 2.7777777777777777, 2.5, ] is 73.22420634920634

Method-2: Java Program to Find Sum of Series a^2 + a^2/2 + a^2/3 + …… + a^2/10 By Using User Defined Method and Without Using Math.pow()

Approach:

  1. Create Scanner class object.
  2. Prompt the user to enter a number and store it in a variable a.
  3. Define a user-defined method to print the series.
  4. Run a for loop from i=1 to a and print Math.pow(a,2)/i.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter value of a
        System.out.print("Enter the value of a: ");
        int a = sc.nextInt();
        // call the method
        printSeriesSum(a);
    }
    
    // method to print the series
    private static void printSeriesSum(int a) 
    {
        // initialize sum to 0
        double sum = 0;
        System.out.print("The sum of the series [");
        // loop to find the sum of the series
        for (int i = 1; i <= 10; i++) 
        {
            sum += Math.pow(a, 2) / i;
            System.out.print((double)(a * a) / i + ", ");
        }
        System.out.print("] is " + sum);
    }
}

Output:

Enter the value of a: 2
The sum of the series [4.0, 2.0, 1.3333333333333333, 1.0, 0.8, 0.6666666666666666, 0.5714285714285714, 0.5, 0.4444444444444444, 0.4, ] is 11.715873015873015

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: