Sum the digits of a number

后端 未结 18 2271
抹茶落季
抹茶落季 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:25

    It only works for three-digit numbers, but it works

    a = int(input())
    print(a // 100 + a // 10 % 10 + a % 10)
    

提交回复
热议问题