
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find sum of N numbers using recursion
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.
To solve with recursion, we need to divide our problem into smaller chunks, then find out solution for that smaller part, and that will be our base condition. In this problem, to find the sum of N numbers using recursion, we can store the numbers in an array and add them using recursion.
Below is a demonstration of the same -
Input: N = 3, arr[] = {10, 20, 30} Output: Sum of n numbers : 60
Sum of N numbers using Recursion
Let's see the following approaches to find the sum of N numbers and they are as follows ?
-
Using Forward Recursion
-
Using Backward Recursion
Using Forward Recursion
In this recursion, we will start from the beginning of the array and keep going in the forward direction until we reach the end of the array.
-
Declare and initialize an array with N numbers.
-
Define a recursive function that takes the array, the current index, and the size.
-
In the function, check if the index has reached the end of the array.
-
If yes, return 0.
-
If not, use the formula: sum = current element + recursive call for next index, sum = array[index] + function(array, index + 1, size)
-
Call the recursive function from the main method, starting with index 0.
-
Store the returned value in a variable.
-
Print the sum of N numbers.
Example
Below is an example of a Java program to find the sum of N numbers using recursion
public class ArraySum { public static int RecursiveSum(int my_array[], int i, int length) { if (i == length) { return 0; } // current element + sum of(n-1) elements return my_array[i] + RecursiveSum(my_array, i + 1, length); } public static void main(String[] args) { int N = 6, my_sum = 0; int my_array[] = {15,30,45,80,100,140}; //function call to do sum my_sum = RecursiveSum(my_array, 0, N); System.out.println("The total of N numbers is : " + my_sum); } }
On compiling, the above program gives you the following output.
Enter the elements of the array : 15 30 45 80 100 140 The total of N numbers is : 410
Backward Recursion
There is also another approach to do this. In this recursion, we start from the end of the array and move backward, and stop when we reach before the first element (index -1).
Example
Below is an example of a Java program to find the sum of N numbers using recursion.
public class ArraySum { //recursive function public static int RecursiveSum(int my_array[], int l){ //base condition if (l == -1) { return 0; } // current element + sum of(n-1) elements return my_array[l] + RecursiveSum(my_array,l-1); } public static void main(String[] args) { int N = 6, my_sum = 0; int my_array[] = {15,30,45,80,100,140}; //function call to do sum my_sum = RecursiveSum(my_array, N-1); System.out.println("The total of N numbers is : " + my_sum); } }
On compiling, the above program gives you the following output.
The total of N numbers is : 410