Simple C# recursive solution


  • 0
    J

    Simple C# recursive solution. Running time 94 ms.

    public int AddDigits(int num) {
            if (num == 0)
                {
                    return 0;
                }
    
                int sum = 0;
    
                while (num > 0)
                {
                    int digit = num % 10;
                    sum += digit;
                    num = num / 10;
                }
    
                if (sum > 9)
                {
                    return AddDigits(sum);
                }
                else
                {
                    return sum;
                }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.