Q:

Write a Python program to Calculate the sum of the digits in an integer

belongs to collection: python basic programs with examples

0

Write a Python program to Calculate the sum of the digits in an integer

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used python 3.7 compiler for debugging purpose.

num = int(input("Input a four digit numbers: "))
a  = num //1000
a1 = (num - a*1000)//100
a2 = (num - a*1000 - a1*100)//10
a3 = num - a*1000 - a1*100 - a2*10
print("The sum of digits in the number is", a+a1+a2+a3)

Result:

Input a four digit numbers: 1234

The sum of digits in the number is 10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

python basic programs with examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Python program to Concatenate N strings... >>
<< Write a Python program to Calculate body mass inde...