This is completely off topic from this groups, but now that we are it:
OK, now that you are trying let me give you my idea, you can compare
and learn from it:
When you are doing recursion you find where the recursion is at, and
what's your base case.
So think that sum of all digits of a given number is the sum of the
last digit plus the sum of all the others digits, but the some of all
the others digits is the sum of the last digit plus... ¡You have
recursion!
Ok, so let's translate into code what exactly means that precious
sentences:
The last digit of a given integer number is calculated with %10, it's
not God-sent either, the a%b returns the reminder of a/b, and a little
bit of math would give you that the reminder of a/10 is always the last
digit of it.
Now the funny thing is n/10. If n is an integer, n/10 drops the decimal
part of it because an integer doesn't hold decimal numbers.
So if you have a function sumDigits() the sentence "the sum of all
other digits (of a given number n)" means sumDigits(n/10)
And of course your base case, the thing that stops the recursion going
forever, is when you have a number that is single-digit. If that's the
case well the sum is the number itself:
Now HOCUS-POCUS:
int sumDigits(int n){
if(n<10)
return n;
else
return n%10 + sumDigits(n/10);
}
I thinks it's also worth to look at a non-recursive function to do
that:
int sumDigits(int n){
int sum;
for(sum=0; n>0; sum+=n%10, n/=10);
return sum;
}