Sum the digits of a number

后端 未结 18 1961
抹茶落季
抹茶落季 2020-11-22 10:52

If I want to find the sum of the digits of a number, i.e.:

  • Input: 932
  • Output: 14, which is (9 + 3 + 2)
18条回答
  •  隐瞒了意图╮
    2020-11-22 11:11

    you can also try this with built_in_function called divmod() ;

    number = int(input('enter any integer: = '))
    sum = 0
    while number!=0: 
        take = divmod(number, 10) 
        dig = take[1] 
        sum += dig 
        number = take[0] 
    print(sum) 
    

    you can take any number of digit

提交回复
热议问题