Skip to main content

How to calculate the sum of digits of a number in Python

How to calculate the sum of digits of a number in Python.

Here's a detailed step-by-step tutorial on how to calculate the sum of digits of a number in Python.

Step 1: Get input from the user

To begin, we need to get a number from the user for which we want to calculate the sum of digits. We can use the input() function to prompt the user and store the input in a variable.

number = int(input("Enter a number: "))

In the above code, input("Enter a number: ") displays the message "Enter a number: " to the user and waits for their input. The int() function is used to convert the input into an integer.

Step 2: Initialize variables

Next, we need to initialize a variable to store the sum of digits. Let's call this variable sum_of_digits and set its initial value to 0.

sum_of_digits = 0

Step 3: Calculate the sum of digits

Now, we will calculate the sum of digits. We can achieve this by using a while loop and repeatedly dividing the number by 10 until it becomes 0.

while number != 0:
digit = number % 10
sum_of_digits += digit
number //= 10

In each iteration of the loop, we extract the rightmost digit of the number using the modulus operator % with 10. We add this digit to the sum_of_digits variable. Then, we update the value of number by floor dividing it by 10 (using the // operator). This removes the rightmost digit from the number.

Step 4: Display the result

Finally, we can display the sum of digits to the user.

print("The sum of digits is:", sum_of_digits)

The print() function is used to output the result to the console.

Complete code example

Here's the complete code example that puts all the steps together:

number = int(input("Enter a number: "))
sum_of_digits = 0

while number != 0:
digit = number % 10
sum_of_digits += digit
number //= 10

print("The sum of digits is:", sum_of_digits)

Additional code examples

Using a for loop

Instead of using a while loop, we can also calculate the sum of digits using a for loop and converting the number to a string.

number = int(input("Enter a number: "))
sum_of_digits = 0

for digit_char in str(number):
digit = int(digit_char)
sum_of_digits += digit

print("The sum of digits is:", sum_of_digits)

In this example, we convert the number to a string using str(number). Then, we iterate over each character in the string using the for loop. We convert each character back to an integer using int(digit_char) and add it to the sum_of_digits variable.

Using recursion

We can also calculate the sum of digits using a recursive function.

def sum_of_digits(number):
if number == 0:
return 0
else:
return number % 10 + sum_of_digits(number // 10)

number = int(input("Enter a number: "))
result = sum_of_digits(number)

print("The sum of digits is:", result)

In this example, the sum_of_digits() function takes the number as an argument. If the number is 0, the function returns 0 (base case). Otherwise, it returns the rightmost digit (number % 10) added to the sum of digits of the remaining number (number // 10). We call the function with the input number and store the result in the result variable. Finally, we display the result to the user.

These are the various ways to calculate the sum of digits in a number using Python. You can choose the method that suits your needs best.