Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

258. Add Digits 🌟

Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Brute Force

Code

class Solution {
public:
    int addDigits(int num)
    {
        string s = to_string(num);
        while (s.size() != 1) {
            num = 0;
            for (auto x : s) num += x - '0';
            s = to_string(num);
        }
        return s[0] - '0';
    }
};

Mathematical solution

Code

class Solution {
public:
    int addDigits(int num) {
        if (num == 0) return 0;
        if (num % 9 == 0) return 9;
        return num % 9;
    }
};