Sum of Digits

    0

    0

    Mahendra Kumar

    beginnerbasicMathsdigitSum

    Calculating the sum of the digits of a given number: Eg: 123 = 1 + 2 + 3 = 6

    def ComputeSum(num): #user-defined function
        sum = 0
        while (num != 0):  
            sum += (num % 10) 
            num //= 10
        return sum
    # take input
    num = int(input('Enter a number: '))
    # calling function & display result
    print('Sum of digits in number =', ComputeSum(num))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.