Search code snippets, questions, articles...

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);
        }
    }

Search Index Data (The code snippet can also be found with below search text)

digits sum recursive
Was this helpful?
0 Comments