In this article, we will understand how to find sum of N numbers using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the value of N : 6 Enter the elements of array : 15 30 45 80 100 140
Output
The desired output would be −
The total of N numbers is : 410
Algorithm
Step 1 - START Step 2 - Declare two integer values namely N , my_sum and i and an integer array ‘my_array’ Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘RecursiveSum is defined which takes two integers as input. The function computes the reminder by re-iterating over the function multiple times, until the base condition is reached. Step 5 - The recursive function ‘RecursiveSum is called and its result is stored Step 6 - Display the result Step 7 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
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
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed 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