Sum of Digits Calculator for Integers

  • Share this:

Code introduction


Calculate the sum of the digits of an integer.


Technology Stack : Built-in type checking, string conversion, digit conversion, summing

Code Type : Function

Code Difficulty : Intermediate


                
                    
def sum_of_digits(n):
    if not isinstance(n, int):
        raise ValueError("Input must be an integer")
    return sum(int(digit) for digit in str(abs(n)))