Get the sum of digits in Python

Question

Input – 342

Output – 9

Avoid using default functions

in progress 0
TheDataMonk 3 years 4 Answers 626 views Grand Master 0

Answers ( 4 )

  1. sum=0
    while (Input>0):
    sum=sum+Input%10
    Input=Input//10
    print(sum)

  2. Def getSum(n):
    Sum=0
    For digit in str(n):
    Sum+= int(digit)
    Return sum

  3. def summ(x):
    x = str(x)
    Sum=0
    for i in x:
    Sum = Sum + int(x)

    return Sum

    print(“sum of digits: “, summ(342))

  4. num=342
    sum=0
    num=[int(i) for i in str(num)]
    for i in num:
    sum=sum+i
    print(sum)

Leave an answer

Browse
Browse