Find Sum of N Numbers Using Recursion in Java



In this article, we will learn how to find the sum of N numbers using recursion in Java. Recursion is when a method calls itself repeatedly until a base condition is met. In Java, each recursive call is placed on the stack until the base case is reached, after which values are returned to calculate the result.

Problem Statement

Given an array of integers, write a Java program to find the sum of all N numbers using recursion.

Input

The user provides the value of N, the number of integers to sum.
The user inputs N integers to be summed.

Output

The program should output the sum of the N numbers.

Steps to find the sum of all N numbers using recursion.

The following are the steps to find the sum of all N numbers using recursion.

  • Declare two integer values and an integer array
  • Read the required values from the user/ define the values
  • A recursive function ?RecursiveSum is defined which takes two integers as input. The function computes the reminder by re-iterating the function multiple times until the base condition is reached.
  • The recursive function ?RecursiveSum is called and its result is stored
  • Output the final sum to the user.

Java program to find the sum of N numbers using recursion

Below is an example of a Java program to find the sum of N numbers using recursion

import java.util.Scanner;
public class ArraySum {
 public static int RecursiveSum(int my_array[], int i,int N){
if (i == N)
 return 0;
return my_array[i] + RecursiveSum(my_array, i + 1,N);
 }
 public static void main(String[] args){
 int N, my_sum, i;
 N = 6;
 my_sum = 0;
 System.out.println("Required packages have been imported");
 Scanner my_scanner = new Scanner(System.in);
 System.out.println("A reader object has been defined ");
 System.out.print("Enter the value of N : ");
 N = my_scanner.nextInt();
     int my_array[] = new int[N];
     System.out.println("Enter the elements of the array :" );
     for ( i = 0 ; i < N ; i++ ){
        my_array[i] = my_scanner.nextInt();
     }
     my_sum = RecursiveSum(my_array, 0, N);
     System.out.println("\n The total of N numbers is : " + my_sum);
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the value of N : 6
Enter the elements of the array :
15
30
45
80
100
140
The total of N numbers is : 410

Java program that accesses and displays the value of a previously defined integer on the console.

The following is an example of a Java program that accesses and displays the value of a previously defined integer on the console.

public class Main {
   public static void main(String[] args) {
      int[] my_array = {15, 20, 25, 30, 35, 40};
      int my_input , i, array_size;
      array_size = 5;
      my_input = 25;
      boolean my_check = false;
      System.out.println("The number is defined as " +my_input);
      System.out.println("The elements in the integer array is defined as :" );
      for ( i = 0 ; i < array_size ; i++ ){
         System.out.print(my_array[i] +" ");
      }
      for ( i = 0 ; i < array_size ; i++ ) {
         if (my_array[i] == my_input) {
            my_check = true;
            break;
         }
      }
     if(my_check)
        System.out.println("\nThe array contains the given value");
     else
        System.out.println("\nThe array doesnot contain the given value");
   }
}

Output

The number is defined as 25
The elements in the integer array is defined as :
15 20 25 30 35
The array contains the given value

Code Explanation

The program defines a recursive function RecursiveSum to calculate the sum of an array. It calls itself until the base case is reached, adding the current element to the sum. In main(), the user enters the array size and elements, and finally, the program displays the sum using RecursiveSum.

Updated on: 2024-11-07T01:05:20+05:30

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements