Issue
I have wrote but I don't know what is wrong, it should return sum of the values of arrays if the array is not-empty but if array is empty it should return zero.
public class Calculation {
int findSum(int A[], int N) {
if (N <= 0)
return 0;
return (findSum(A, N - 1) + A[N - 1]);
}
int main() {
int A[] = {1, 2, 3, 4, 5};
int N = sizeof(A) / sizeof(A[0]);
System.out.print("Sum = " + findSum(A, N));
return 0;
}
}
Solution
You are using C/C++ code in Java. This is working for Java
public class Calculation {
static int findSum(int[] A, int N) {
if(N<=0) {
return 0;
}
int sum = 0;
for(int i:A) {
sum+=i;
}
return sum;
}
public static void main(String args[]) {
int[] A = {1,2,3,4,5};
int N = A.length;
System.out.println("Sum of x+y = " + findSum(A, N));
}
}
Answered By - Ayman Patel