Advertisements
Advertisements
Question
Write a program to find the sum of all digits of the given number.
Code Writing
Advertisements
Solution
Extract the last digit using % 10, add it to sum, and remove it using // 10.
n = int(input("Enter any number: "))
sum_of_digits = 0
while n > 0:
sum_of_digits += n % 10
n //= 10
print("Sum of digits =", sum_of_digits)shaalaa.com
Is there an error in this question or solution?
