Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Print Fibonacci Series by Using Recursion
In this program we are going to see how to find sum of all numbers in an Array by using Recursion in Java programming language.
Java Program to Find Sum of All Numbers in an Array by Using Recursion
Lets assume there is an array say A which has 5 elements {1,2,3,4,5}
So, sum of the elements of array A = 1+2+3+4+5 = 15
Now let’s see different ways to find sum of all numbers in an Array by using Recursion.
Method-1: Java Program to Find Sum of All Numbers in an Array by Using Static Input and Recursion
Approach:
- Declare and initiate an integer variable Array say ‘
A
’. - Call a user defined method
calculateSum()
and pass the array ‘A
’ and the size of the array i.e. ‘A.length
’ as parameter. - Inside the user defined method we will check the size of the Array. If the size of the array is
<= 0
then the method returns 0 else it calls the same method ascalculateSum(A, n-1) + A[n-1]
and return the value to the main method. - Now the value of the user defined method
calculateSum()
is stored in an integer variable say ‘sum
’. - Print the value of
sum
.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { //declare and initialize an array A int A[] = {1,2,3,4,5}; //call the user defined method calculateSum() //and store the result value inside an integer variable say ‘sum’ int sum = calculateSum(A, A.length); //print the result System.out.println("The sum of the Array is: "+sum); } //calculateSum() method static int calculateSum(int A[], int n) { //checking if array has no element then returning 0 as result if (n<= 0) return 0; //else array has some elements //and adding all the array elements by calling the calculateSum() method recursively else //using recursion technique return calculateSum(A,n-1) + A[n-1]; } }
Output: The sum of the Array is: 15
Method-2: Java Program to Find Sum of All Numbers in an Array by Using Dynamic Input and Recursion
Approach:
- Declare an integer variable say ‘
n
‘ which holds the array size, take it’s value input from user. - Declare an integer variable Array say ‘
A
’ with array size as ‘n
‘ - Take input of array elements from user by using a for loop.
- Call a user defined method
calculateSum()
and pass the array ‘A
’ and the size of the array i.e. ‘A.length
’ as parameter. - Inside the user defined method we will check the size of the Array. If the size of the array is
<= 0
then the method returns 0 else it calls the same method ascalculateSum(A, n-1) + A[n-1]
and return the value to the main method. - Now the value of the user defined method
calculateSum()
is stored in an integer variable say ‘sum
’. - Print the value of
sum
.
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // create a scanner class object Scanner s = new Scanner(System.in); System.out.println("Enter number of elements you want in array:"); //take the array size input from user int n = s.nextInt(); // declare the array int A[] = new int[n]; System.out.println("Enter all the elements:"); //Take input of all the elements sequentially from user for(int i = 0; i < n; i++) { A[i] = s.nextInt(); } //call the user defined method calculateSum() //and store the result value inside an integer variable say ‘sum’ int sum = calculateSum(A, A.length); //print the result System.out.println("Sum of the Array elements: "+sum); } //calculateSum() method static int calculateSum(int A[], int n) { //checking if array has no element then returning 0 as rsult if (n<= 0) return 0; //else array has some elements //and adding all the array elements by calling the calculateSum() method recursively else //using recursion technique return calculateSum(A,n-1) + A[n-1]; } }
Output: Enter number of elements you want in array: 4 Enter all the elements: 10 20 30 40 Sum of the Array elements: 100
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: