Decode School

Decode School - The School for Problem Solvers
Need Assignment / Project Guidance (or) Training in Python?

Sum of Digits of the number using Python

Get a number and sum the digits of the number.

Sample Input 1:

Enter the number: 234

Sample Output 1:


Program or Solution


num = int(input("enter a number"))

n = num

total = 0

while n != 0:

    rem = n % 10

    total = total + rem

    n = n // 10


Program Explanation

initialize total is equal to 0

calculate the remainder of a number by doing number % 10,  add the remainder to total and divide the number by the 10 and repeat the above steps till number becomes zero.

Print the total, you will get the sum of digits of the number.