English

Write a program to find the sum of all digits of the given number.

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?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 20. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×