Add Digits
Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
Follow up: Could you do it without any loop/recursion in O(1) runtime?
Example 1
Input
num = 38Output
2The process is 38 --> 3 + 8 --> 11, then 11 --> 1 + 1 --> 2, and since 2 has only one digit, return it.
Example 2
Input
num = 0Output
0The number 0 already has only one digit, so return 0.
Constraints
- 0 <= num <= 2^31 - 1