Sum of digits of a number in Python
In this post, you will learn how to calculate the sum of digits of a number using Python programming language. Such a type of question is generally asked in a programming interview. In Python, there are different ways to find the sum of digits of a number. Here, we have mentioned most of them-
Sum of digits of a number using for loop
In the given example, we have asked the user to provide the number and used the for loop to calculate the sum of digits of a number.
# Sum of digits of a number in Python
# using for loop
num = int(input("Please enter number: "))
#Initialize sum with 0
sum=0
#Loop through the digits
for x in str(num):
sum=sum+int(x)
print("Sum of digits: ",sum)
Output of the above code:
Please enter number: 53434
Sum of digits: 19
Sum of digits of a number using while loop
In the given Python program, we asked the user to enter a number, then used the while loop to calculate the sum of digits.
# Sum of digits of a number in Python
# using for loop
num = int(input("Please enter number: "))
#Initialize sum with 0
sum=0
#Loop through the digits
while(num > 0):
digit = num%10
sum = sum+digit
num = num//10
print("The total sum of digits is:",sum)
Output of the above code:
Please enter number: 63342
The total sum of digits is: 18
Sum of digits of a number using function
In the given Python program, we have defined a function to calculate the sum of digits of a number entered by the user. First, we ask the user to initialise the number, then find all the digits and calculate their sum by calling the function sumOfDigits().
# Sum of digits of a number in Python
# using function
def sumOfDigits(num):
#Initialize sum with 0
sum =0
while num>0:
sum += num%10
num = num//10
return sum
num = int(input("Please enter number: "))
print("The total sum of digits : ",sumOfDigits(num))
Output of the above code:
Please enter number: 68579
The total sum of digits : 35
Sum of digits of a number using recursion
A recursion function is a function that is called by itself. In the given example, we use the recursion function to find the sum of digits of a given number. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion.
# Sum of digits of a number in Python
# using function
def sumOfDigits(n):
# Check if n is less than 10, if true return n
if n< 10:
return n
else:
# divide the number by 10
# and calculate the remainder
return n%10 + sumOfDigits(n//10)
# Input from the user
num = int(input("Please enter number: "))
# Call function and pass input as a parameter
print("The total sum of digits : ",sumOfDigits(num))
Output of the above code:
Please enter number: 865577
The total sum of digits : 38
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary