Sum of Digits Calculator for Non-negative Integers

  • Share this:

Code introduction


Calculate the sum of the digits of a non-negative integer


Technology Stack : Built-in library

Code Type : Function

Code Difficulty :


                
                    
def sum_of_digits(num):
    if not isinstance(num, int) or num < 0:
        return "Input must be a non-negative integer"
    
    return sum(int(digit) for digit in str(num))