import javax.swing.*;
import java.util.Arrays;
public class Recursionsum {
public static void main(String[] args)throws Exception {
int []val=new int[20];
int i=Integer.parseInt(JOptionPane.showInputDialog("En ter a number:"));
int sum = addArray(val, 0,0);
System.out.println(sum);
}
public static int addArray(int[] a, int index, int sum) {
if(index == a.length-1) {
return sum + a[a.length-1];
} else {
return addArray(a, index+1, sum+a[index]);
}
}
}
I have my problem with this code because the output of this is wrong. Actually the task of this code, is to ask the user a number around 20 numbers. But then, the problem is when the user enter for example:
(2 3 4 2 1)5 numbers the output is zero that actually very wrong.
i want this code to add the all values enter by the user. so the(2 3 4 2 1)total of this is 12 and it will display the result.
Thank you.

