public class Main {
    public static void main(String[] args) {

        int number = -123456;

        if(number < 0)
            number = -number;

        int sum = 0;
        while (number > 0) {
            sum += number % 10;
            number /= 10;
        }
        System.out.println(sum);
    }
}
  • Time Complexity: O(log₁₀N) ; N is the number.
  • Space Complexity: O(1).