Add Digits

Desicription

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Example:

1
2
3
4
Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
Since 2 has only one digit, return it.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
private:
int getLength(int num) {
return static_cast<int>(log10(num) + 1);
}
public:
int addDigits(int num) {
if(getLength(num) <= 1) {
return num;
}
int sum = 0;
while(num) {
sum += num % 10;
num /= 10;
}
return addDigits(sum);
}
};