मराठी

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

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 1: Review of Phython - EXERCISE [पृष्ठ २७]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
पाठ 1 Review of Phython
EXERCISE | Q 20. | पृष्ठ २७
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×