- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfArrayUsingRecursion.java
More file actions
Latest commit
31 lines (27 loc) · 911 Bytes
/
Copy pathSumOfArrayUsingRecursion.java
File metadata and controls
31 lines (27 loc) · 911 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
packageRecursionAndBacktracking;
importjava.util.Scanner;
publicclassSumOfArrayUsingRecursion {
publicstaticintsumOfArray(int[] a, intsize){
intsum = 0;
if(size <= 0){
return0;
}
sum += sumOfArray(a, size-1) + a[size-1];
returnsum;
}
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.println("Enter t:");
intt = sc.nextInt();
while (t-- > 0) {
System.out.println("Enter the size of the array:");
intsize = sc.nextInt();
System.out.println("Enter the elements of the array:");
int[] array = newint[size];
for (inti = 0; i < size; i++) {
array[i] = sc.nextInt();
}
System.out.println(sumOfArray(array, size));
}
}
}