java

Recursive Sum of Digits

Calculates the sum of the digits of a number recursively

static int digitsSum(int n) {
        if (n < 0) {
            return -1;
        }
        if (n < 10) {
            return n;
        } else {
            return n % 10 + digitsSum(n / 10);
        }
    }
Was this helpful?