I'm working on a simple recursion function that takes a number and then adds the digits together, say 12345 = 1+2+3.....=15 I have that part:
int DigitSum(int number, int sum){
//if the remainder of the number / 10 is 0, return the sum
if(number % 10 <= 0){
return sum;
}
//else, sum adds to itself the decimal remainder, then shaves off the decimal by number/10 (int loses decimal)
//when calling itself.
else {
sum = sum + number % 10;
return DigitSum(number / 10, sum);
}
}
but now I need to write a second function that used the first one to add THOSE digits together, so say I input 12345 again, it would use the first function to get 15, but then add those digits to get 6.
I know I am way over thinking this, but could I get a hint? I tried this:
int DigitSum2(int number, int sum){
int sum2;//if the remainder of the number / 10 is 0, return the sum
if(number % 10 <= 0){
return DigitSum(sum, sum2);
}
//else, sum adds to itself the decimal remainder, then shaves off the decimal by number/10 (int loses decimal)
//when calling itself.
else {
sum = sum + number % 10;
return DigitSum(number / 10, sum);
}
}
but of course did not work.