-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathArraySum.java
More file actions
31 lines (26 loc) · 879 Bytes
/
ArraySum.java
File metadata and controls
31 lines (26 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;
/**
* Recursive Array Sum
* @author MadhavBahl
* @date 18/01/2019
*/
public class ArraySum {
public static int findArraySum (int arr[], int num) {
if (num <= 0) return 0;
return arr[num-1] + findArraySum(arr,num-1);
}
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("/* ===== Sum of Array elements using recursion ===== */");
// Input the array
System.out.print("\nEnter the number of elements in the array: ");
int n = input.nextInt();
int arr[] = new int[n];
for (int i=0; i<n; i++) {
System.out.print("Enter arr[" + i + "]: ");
arr[i] = input.nextInt();
}
// Print the product
System.out.println("The sum of numbers is: " + findArraySum(arr, n));
}
}