In: Computer Science
4. Write a program to compute the sum of digits of an integer. For example, given the integer 35931, your program should output 21. (21 is the sum 3 + 5 + 9 + 3 + 1)
PYTHON
Solution:
number=int(input("Enter an integer:"))
total=0
temp=number
while number!=0:
#Extracting last digit of the number
d=number%10
#Adding the extracted digit to total
total=total+d
#Dividing number by 10 to extract the next digit
number=number//10
print("Sum of digits of",temp,"is",total)
Output:
Output(Screenshot):