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

class Solution {
    public int addDigits(int num) {
        while(num >= 10) {
            int next = 0;
            while(num > 0) {
                next += num % 10;
                num /= 10;
            }
            num = next;
        }

        return num;
    }
}

results matching ""

    No results matching ""