how to make this thing works? sum of array recursion. import java.util.ArrayList
ID: 3742913 • Letter: H
Question
how to make this thing works?
sum of array recursion.
import java.util.ArrayList;
public class sum
{
public static int num (int V[],int N){
V= new int [20];
int Sum =0;
Sum +=N;
{
if (N == 0){
return 0;
}
else
return V[N]+ Sum(V, N-1);
}
}
public static void main(String[] args)
{
int V[] = { 1, 2, 3, 4, 5 };
System.out.print(num(V,N-1));
}
}
Explanation / Answer
public class sum { public static int num (int V[],int N){ if(N >= 0) { return V[N] + num(V, N-1); } else { return 0; } } public static void main(String[] args) { int V[] = { 1, 2, 3, 4, 5 }; System.out.print(num(V,V.length-1)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.