Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Find Sum of N Numbers Using Recursion

In this tutorial, we will see how to find the sum of N numbers using recursion. A recursive function is a function that calls itself. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the numbers: 6 7 4 5 3

Output: The sum of all the entered numbers is: 25

Program 1: Find Sum of N Numbers Using Recursion

In this program, we will see how to calculate the sum of N numbers using recursion. Here, we will consider the length variable in the function as the changing parameter.

Algorithm:

  1. Start

  2. Create an instance of the Scanner Class.

  3. Declare a variable to store the length of the array.

  4. Ask the user to initialize the array size.

  5. Declare a variable to store the array elements.

  6. Ask the user to initialize the array elements.

  7. Call a recursive function to calculate the sum.

  8. Consider the length variable as the changing parameter in the function.

  9. Call the function recursively to calculate the sum.

  10. Display the calculated sum.

  11. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to Find Sum of N Numbers Using Recursion
import java.util.*;
  
public class Main 
{
    // recursive function
     public static int calculate_sum(int arr[], int length)
    {
        // base condition - when reached -1 index return 0
        if (length == -1) 
        {
            return 0;
        }
        // Call the function recursively to calculate the sum
        return arr[length] + calculate_sum(arr,length - 1);
        
    }
    //Driver Code
    public static void main(String[] args)
    {
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array size: ");
        int n=sc.nextInt();
        int total_sum = 0;
        //Array Creation and Initialization
        int arr[] = new int[n];
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        // call function to calculate sum
        total_sum = calculate_sum(arr, n-1);
        System.out.println("The total of N numbers is : "+ total_sum);
    }
}


Enter the array size: 5
Enter the array elements: 4 7 6 5 3
The total of N numbers is: 25

Program 2: Find Sum of N Numbers Using Recursion

In this program, we will see how to calculate the sum of N numbers using recursion. Here, we will start the recursion from the forward direction and reach and hit the base condition at the end/last position.

Algorithm:

  1. Start

  2. Create an instance of the Scanner Class.

  3. Declare a variable to store the length of the array.

  4. Ask the user to initialize the array size.

  5. Declare a variable to store the array elements.

  6. Ask the user to initialize the array elements.

  7. Call a recursive function to calculate the sum.

  8. Start the recursion from the forward direction.

  9. Call the function recursively to calculate the sum.

  10. Display the calculated sum.

  11. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java Program to Find Sum of N Numbers Using Recursion
import java.util.*;
  
public class Main 
{
    // recursive function
    public static int calculate_sum(int arr[], int i, int length)
    {
        // base condition - when reached end of the array
        // return 0
        if (i == length) {
            return 0;
        }
        // recursive condition - current element + sum of
        // (n-1) elements
        return arr[i]
         + calculate_sum(arr, i + 1,length);
        
    }
    //Driver Code
    public static void main(String[] args)
    {
        //Create an instance of the Scanner Class
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array size: ");
        int n=sc.nextInt();
        int total_sum = 0;
        //Array Creation and Initialization
        int arr[] = new int[n];
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();
        }
        // call function to calculate sum
        total_sum = calculate_sum(arr,0,n);
        System.out.println("The total of N numbers is : "+ total_sum);
    }
}


Enter the array size: 5
Enter the array elements: 2 6 4 7 8
The total of N numbers is: 27