Sum of Digits of a Number in Python using For loop
ADVERTISEMENTS
Sum of digits of a number in python using for loop. In this article, you will learn how to find the sum of digits of a number in python using for loop.
Sum of Digits of a Number
An integer number of 4 digits is 2368 ==> 2 + 3 + 6 + 8 ==> 19
Source Code
# Sum of Digits of a Number in Python using For loop
print ("Enter the integer number::\n")
x, sum, m = int(input()), 0, None
print ("The sum of ", x, " digits is = ", end="")
while x > 0:
m = x % 10
m = int (m)
sum = sum + m
x = x / 10
x = int (x)
print (sum, "\n")
Output
Enter the integer number::
1459
The sum of 1459 digits is = 19