Sum of Digits of a Number in Python

In this post, we will write a program to find the sum of digits of an integer number in Python. We can use the while loop to write the program. We can also develop a Python program to calculate the sum of digits without using the loop. The integer numbers will be given as input and the python program to compute the sum of digits in numbers using various methods.

Example of the sum of digits of an integer number:- 54321 = 5+4+3+2+1 = 15

Sum of Digits in Python

This python program using a while loop to compute the sum of digits in a number. We can also take the help of a user-defined function. A function is a block of code that performs a specific task. We will take integer numbers while declaring the variables. Python program to compute the sum of digit using while loop and finally, the result will be displayed on the screen.

# Python program to compute sum of digits in number

def ComputeSum(num): #user-defined function
    sum = 0
    while (num != 0):  
        sum += (num % 10) 
        num //= 10
        
    return sum

# take input
num = int(input('Enter a number: '))

# calling function & display result
print('Sum of digits in number =', ComputeSum(num))

Output for the different input values:-

Enter a number: 12345
Sum of digits in number = 15

Enter a number: 8723287
Sum of digits in number = 37

Python Program to Find Sum of Digits using Recursion

We can also use the recursion technique to compute the sum of digits in numbers. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

# Python program to compute sum of digits in number

def ComputeSum(num): #user-defined function
    return 0 if num == 0 else int(num % 10) + ComputeSum(int(num / 10))

# take input
num = int(input('Enter a number: '))

# calling function & display result
print('Sum of digits in number =', ComputeSum(num))

Output:-

Enter a number: 4674346
Sum of digits in number = 34

Using str() and int() methods

We will use str() and int() methods to compute the sum of digits of a number. The str() is to convert the number to a string and the int() is to convert the string digit to an integer. We will also use for loop to compute the sum of digits.

# Python program to compute sum of digits in number

def ComputeSum(num): #user-defined function
    sum = 0
    for digit in str(num):
        sum += int(digit)
    return sum

# take input
num = int(input('Enter a number: ')

# calling function & display result
print('Sum of digits in number =', ComputeSum(num))

Output:-

Enter a number: 987654321
Sum of digits in number = 45

Program Using sum() methods

In the previous program, we will use str() and int() methods but in this program, compute the sum of digits uses the sum() method. The sum() method is used to sum of numbers in the list. Str() is covert the number to string and strip() is converted to list of number and map() method resp. Then, sum() is computed as the sum of digits in a number.

# Python program to compute sum of digits in number

def ComputeSum(num): #user-defined function
    x = str(num) 
    list_num = list(map(int, x.strip())) 
    return sum(list_num)
    
# take input
num = int(input('Enter a number: '))

# calling function & display result
print('Sum of digits in number =', ComputeSum(num))

Output:-

Enter a number: 54321
Sum of digits in number = 15

Also See:- Find Largest of 3 Numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *