Java Program to Find Sum of N Numbers Using Recursion
Last Updated :
19 Jul, 2022
Recursion is a process by which a function calls itself repeatedly till it falls under the base condition and our motive is achieved.
To solve any problem using recursion, we should simply follow the below steps:
- Assume/Identify the smaller problem from the problem which is similar to the bigger/original problem.
- Decide the answer to the smallest valid input or smallest invalid input which would act as our base condition.
- Approach the solution and link the answer to the smaller problem given by the recursive function to find the answer to the bigger/original problem using it.
Here, we are illustrating the total Sum using recursion can be done using storing numbers in an array, and taking the summation of all the numbers using recursion.
Example
Input: N = 5, arr[] = {70, 60, 90, 40, 80}
Output: Total Sum = 340
Input: N = 8, arr[] = {8, 7, 6, 5, 4, 3, 2, 1}
Output: Total Sum = 36
Approach:
Now, we will apply the approach discussed above in this question to calculate the sum of all elements recursively.
- Smaller problem will be the array from index 1 to last index.
- Base condition will be when the index will reach the length of the array.ie out of the array that means that no element exist so the sum returned should be 0.
- Now, our task is to solve the bigger/ original problem using the result calculated by smaller problem. So, to do that as we know smaller problem is from index1 to last index , so if we get the sum of this problem then after that for the whole sum of array we just need to add the first element / ith element to that answer and return our final answer.
Below is the implementation of the above approach.
Example:
Java
// Java program to calculate the sum of
// the elements of the array recursively
import java.io.*;
class GFG {
// recursive function
public static int calculate_sum_using_recursion(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_using_recursion(arr, i + 1,length);
}
public static void main(String[] args)
{
int N = 5, total_sum = 0;
// create 1-D array to store numbers
int arr[] = { 89, 75, 82, 60, 95 };
// call function to calculate sum
total_sum = calculate_sum_using_recursion(arr, 0, N);
// print total sum
System.out.println("The total of N numbers is : "
+ total_sum);
}
}
OutputThe total of N numbers is : 401
Time complexity: O(N)
Auxiliary Space: O(N)
Approach 2:
We can apply recursion by not just one way but there can be one or more than one ways to solve a single problem using recursion.
In the above approach, we started recursion from forward direction and reached and hit the base condition at the end/last position.
In this approach, we will consider the length variable in the function as the changing parameter, where length variable will start from the last position and the base case will hit reaching to the front out of bound index which is -1.
Example:
Java
// Java program to calculate the sum of
// the elements of array using recursion
import java.io.*;
class GFG {
// recursive function
public static int calculate_sum_using_recursion(int arr[], int length)
{
// base condition - when reached -1 index
// return 0
if (length == -1) {
return 0;
}
// recursive condition - current element + sum of
// (n-1) elements
return arr[length]
+ calculate_sum_using_recursion(arr,length - 1);
}
public static void main(String[] args)
{
int N = 8, total_sum = 0;
// create 1-D array to store numbers
int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
// call function to calculate sum
total_sum = calculate_sum_using_recursion(arr, N - 1);
// print total sum
System.out.println("The total of N numbers is : "
+ total_sum);
}
}
OutputThe total of N numbers is : 36
Time complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Java Program to Compute the Sum of Numbers in a List Using Recursion ArrayList is a part of the Collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. I
5 min read
Java Program to Find Sum of Natural Numbers Using While Loop While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test conditi
3 min read
Java Program to Compute the Sum of Numbers in a List Using For-Loop Given a list of numbers, write a Java program to find the sum of all the elements in the List using for loop. For performing the given task, complete List traversal is necessary which makes the Time Complexity of the complete program to O(n), where n is the length of the List. Example: Input : List
2 min read
Java Program to Compute the Sum of Numbers in a List Using While-Loop The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion
2 min read
Java Program For Adding Two Numbers Represented By Linked Lists- Set 2 Given two numbers represented by two linked lists, write a function that returns the sum list. The sum list is linked list representation of the addition of two input numbers. It is not allowed to modify the lists. Also, not allowed to use explicit extra space (Hint: Use Recursion). Example : Input:
8 min read
Java Program to Find Sum of Array Elements Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th
3 min read
Java Program To Find The Sum Of Last N Nodes Of The Given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input: 10->6->8->4->12, n = 2 Output: 16 Sum of last two nodes: 12 + 4 = 16 Input: 15->7->9->5->16->14, n = 4
10 min read
Program to find Nth term of the series 2, 4, 3, 4, 15... Given a number N. The task is to write a program to find the Nth term in the below series as follows: 2, 4, 3, 4, 15... Examples: Input: N = 5 Output: 15 Explanation: For N = 5, Nth term = ( N * ( (N%2) + (N%3) ) = ( 5 * ( (5%2) + (5%3) ) = ( 5 * ( 1 + 2 ) = 15Input: N = 4 Output: 4 Generalized Nth
4 min read
Java Program to Increment All Element of an Array by One Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar
4 min read
C Program to Find Sum of Natural Numbers using Recursion Natural numbers include all positive integers from 1 to infinity. There are multiple methods to find the sum of natural numbers and here, we will see how to find the sum of natural numbers using recursion. Example Input : 5Output : 15Explanation : 1 + 2 + 3 + 4 + 5 = 15 Input : 10Output : 55Explanat
2 min read