Sum of Digits in an Integer
Given a non-negative int n, return the sum of its digits recursively (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
sumDigits(126) → 9
sumDigits(49) → 13
sumDigits(12) → 3
----------------------------------------------------------
public int sumDigits(int n) {
int Count=0;
if(n/10<10){
Count=(n%10)+(n/10);
}
else{
Count=sumDigits(n/10)+sumDigits(n%10);
}
return Count;
}
sumDigits(126) → 9
sumDigits(49) → 13
sumDigits(12) → 3
----------------------------------------------------------
public int sumDigits(int n) {
int Count=0;
if(n/10<10){
Count=(n%10)+(n/10);
}
else{
Count=sumDigits(n/10)+sumDigits(n%10);
}
return Count;
}
Comments
Post a Comment