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);
        }
    }
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

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

digits sum recursive
Was this helpful?
0 Comments