I'm attempting to create a method that will calculate the sum of a an integer, as the integer is broken down in to single integers.
E.g. 2546 becomes 2, 5, 4, 6. Then I plus those all together.
2 + 5 + 4 + 6 = 17
The method will run recursively.
I'd made this program non-recursively, but in that one, I had a variable to store a sum of the calculation.
public static int calcSum(int n){
if (n>0)
return ((n%10) + calcSum(n/10));
else
return 0;
}
The program works, I just don't understand how the sum is stored.