Java Program to Find Sum of Series 1^2+2^2+3^2+…+n^2 by using Recursion

Prerequisite: Recursion in Java

In the previous article, we have discussed about Java Program to Find Digital Roots of a Number by Using Recursion

In this program we are going to see how to find sum of the series by using Recursion by Java programming language.

Java Program to Find Sum of Series 1^2+2^2+3^2+…+n^2 by using Recursion

Let’s understand it with an example.

Lets assume the nth term of the series n = 4

Sum of the series = 1^2+2^2+3^2+4^2 = 1+4+9+16 = 30

Now let’s see different ways to find sum of the series by using Recursion.

Method-1: Java Program to Find Sum of Series 1^2+2^2+3^2+…+n^2 By Using Static Input and Recursion

Approach:

  • Declare and initialize an integer variable say ‘n
  • Call a user defined method sumSeries() method and pass ‘n’ as parameter to find sum of the series .
  • Inside the user defined method we will check if the nth term is 0 or not. If the nth term is 0 then the sum of the series is 0 else find the sum of the series by calling sumSeries() method recursively.
  • Now the value of the user defined method sumSeries() is stored in an integer variable say ‘sum’.
  • Print the value of the sum of the series.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // declare and initialize an integer variable ‘n’ = 4
        int n = 4;
        //call sumSeries() method to find the sum of the series
        int sum = sumSeries(n);
        // print the result
        System.out.println("The sum of the series is: "+sum);
    }
    
    //sumSeries() method
    static int sumSeries(int n)
    {
        // if the nth term is 0 then the sum of the series is 0 
        if(n == 0)
            return 0;
        // else find the sum of the series by calling sumSeries() method recursively
        else
            return (n*n) + sumSeries(n-1);
    }
}
Output:

The sum of the series is: 30

Method-2: Java Program to Find Sum of Series 1^2+2^2+3^2+…+n^2 By Using User Input and Recursion

Approach:

  • Declare an integer variable say ‘n
  • Prompt the user to enter a number.
  • Call a user defined method sumSeries() method and pass ‘n’ as parameter to find sum of the series .
  • Inside the user defined method we will check if the nth term is 0 or not. If the nth term is 0 then the sum of the series is 0 else find the sum of the series by calling sumSeries() method recursively.
  • Now the value of the user defined method sumSeries() is stored in an integer variable say ‘sum’.
  • Print the value of the sum of the series.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the nth term of the series:");
        int n = s.nextInt();
        //defined sumSeries() method to find the sum of the series
        int sum = sumSeries(n);
        // print the result
        System.out.println("The sum of the series is: "+sum);
    }
    
    //sumSeries() method
    static int sumSeries(int n)
    {
        // if the nth term is 0 then the sum of the series is 0 
        if(n == 0)
            return 0;
        // else find the sum of the series by calling sumSeries() method recursively
        else
            return (n*n) + sumSeries(n-1);
    }
}
Output:

Enter the nth term of the series:
9
The sum of the series is: 285

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.

Related Java Programs: