KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Write a program to enter a number and display the sum of its digits.

Sample Input: 642

Output: 12 (6+4+2 = 12)

Python Control Flow

3 Likes

Answer

n = int(input("Enter a number: "))
s = 0

while(n != 0):
    d = n % 10
    s = s + d
    n = n // 10

print("Sum of digits =", s)

Output

Enter a number: 642
Sum of digits = 12

Answered By

3 Likes


Related Questions