Python Program to Find Sum of Digits of a Number
You can find the sum of digits of a number in Python by converting it to a string and summing the integer value of each character using
sum(int(digit) for digit in str(number)).Examples
Input123
Output6
Input405
Output9
Input0
Output0
How to Think About It
To find the sum of digits, think of the number as a sequence of single digits. Extract each digit one by one, convert it to a number if needed, and add it to a total sum. Repeat until all digits are added.
Algorithm
1
Get the input number.2
Convert the number to a string to access each digit.3
For each character in the string, convert it back to an integer.4
Add each integer digit to a running total.5
Return or print the total sum.Code
python
number = int(input("Enter a number: ")) sum_of_digits = sum(int(digit) for digit in str(abs(number))) print("Sum of digits:", sum_of_digits)
Output
Enter a number: 123
Sum of digits: 6
Dry Run
Let's trace the input 123 through the code
1
Input number
number = 123
2
Convert to string
str(number) = '123'
3
Convert each digit to int and sum
int('1') + int('2') + int('3') = 1 + 2 + 3 = 6
4
Print result
Sum of digits: 6
| Digit | Integer Value | Running Sum |
|---|---|---|
| '1' | 1 | 1 |
| '2' | 2 | 3 |
| '3' | 3 | 6 |
Why This Works
Step 1: Convert number to string
Converting the number to a string lets us access each digit individually as a character.
Step 2: Convert each character to integer
Each character digit is converted back to an integer so we can perform arithmetic.
Step 3: Sum all digits
Adding all integer digits together gives the total sum of the digits.
Alternative Approaches
Using a while loop and modulo
python
number = int(input("Enter a number: ")) sum_of_digits = 0 number = abs(number) while number > 0: sum_of_digits += number % 10 number //= 10 print("Sum of digits:", sum_of_digits)
This method uses math operations without converting to string, which can be more efficient for very large numbers.
Using recursion
python
def sum_digits(n): n = abs(n) if n == 0: return 0 return n % 10 + sum_digits(n // 10) number = int(input("Enter a number: ")) print("Sum of digits:", sum_digits(number))
This recursive method breaks down the problem into smaller parts but may be less efficient for very large numbers.
Complexity: O(n) time, O(n) space
Time Complexity
The program processes each digit once, so time grows linearly with the number of digits (n).
Space Complexity
Converting the number to a string uses extra space proportional to the number of digits (n).
Which Approach is Fastest?
The modulo and division method uses constant space and is generally faster for very large numbers, while string conversion is simpler and more readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| String conversion and sum | O(n) | O(n) | Simplicity and readability |
| Modulo and division loop | O(n) | O(1) | Efficiency with large numbers |
| Recursion | O(n) | O(n) | Learning recursion, less efficient |
Convert the number to a string to easily loop over each digit.
Forgetting to convert each digit back to an integer before summing causes errors or incorrect results.