# 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!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.knowprogram.com/python/sum-of-digits-of-number-python/", "fetch_time": 1701994926000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-50/segments/1700679100705.19/warc/CC-MAIN-20231207221604-20231208011604-00705.warc.gz", "warc_record_offset": 939507105, "warc_record_length": 24648, "token_count": 936, "char_count": 3832, "score": 3.9375, "int_score": 4, "crawl": "CC-MAIN-2023-50", "snapshot_type": "latest", "language": "en", "language_score": 0.7444767951965332, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00052-of-00064.parquet"}