Advertisements
Advertisements
प्रश्न
Write a program to find the sum of all digits of the given number.
कोड लेखन
Advertisements
उत्तर
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
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
