number = 123 # the number you want summed up
sum_of_digits = 0
for digit in str(number):
sum_of_digits += int(digit)
print(sum_of_digits) # printing the final sum of number
num = int(input("Enter the number: "))
sum_of_digits = 0
while num > 0:
digit = num % 10
num //= 10
sum_of_digits += digit
print("The sum of digits is", sum_of_digits)
num = input("Enter your number: ")
result = 0
for n in str(num):
result += int(n)
print("Result: ",result)
#code by fawlid
# Python 3 program to
# compute sum of digits in
# number.
# Function to get sum of digits
def getSum(n):
sum = 0
while (n != 0):
sum = sum + int(n % 10)
n = int(n/10)
return sum
# Driver code
n = 687
print(getSum(n))
# Python program to
# compute sum of digits in
# number.
# Function to get sum of digits
def getSum(n):
strr = str(n)
list_of_number = list(map(int, strr.strip()))
return sum(list_of_number)
n = 12345
print(getSum(n))