I am trying to make a recursive function which should return the sum of digits. The function should have only one argument. So far I have this;
public int sumDigits(int n) {
if(n%10 == n) // last digit remains
return n;
else{
int rightdigit;
rightdigit = n%10; // taking out the right most digit
return rightdigit + (n/10); // adding it to everything on left
}
}
The function works for some values, specifically 2 digit numbers. But It gives out really odd values for some numbers like 730 which comes out to be 73 when it should be 10.
I have worked it out on paper but cant figure out where I am going wrong. Any help would be much appreciated.