Sum the Digits of a Given Number - Python
Last Updated :
24 Feb, 2025
The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15.
Using modulo (%)
This method efficiently extracts each digit using the modulus (%) and integer division (//) operations, avoiding unnecessary string conversions. It is the fastest and most memory-efficient approach, making it ideal for performance-focused applications.
Python
n = 12345
sum = 0
while n > 0:
sum += n % 10 # extract last digit
n //= 10 # remove last digit
print(sum)
Explanation: This code iterates through the digits of n, extracting each last digit using % 10, adding it to sum and removing it with // 10 until n becomes 0.
Using recursion
This method uses recursion to break the problem into smaller subproblems. It follows the same mathematical approach but introduces function call overhead, making it slightly less efficient than the iterative method. It is useful when a recursive solution is preferred.
Python
# recursive function
def fun(n):
if n == 0:
return 0 # base case
return (n % 10) + fun(n // 10)
n = 12345
print(fun(n))
Explanation: fun(n) recursively sums the digits of n. If n is 0, it returns 0. Otherwise, it adds the last digit (% 10) to the sum of a recursive call on the remaining digits (// 10), repeating until n becomes 0.
Using map()
map() apply the int() conversion directly, reducing memory usage. It provides a more functional programming style but still involves string conversion and making it less efficient than mathematical methods.
Python
n = 12345
sum = sum(map(int, str(n)))
print(sum)
Explanation: This code converts n to a string, maps each digit to an integer and sums them using sum().
Using reduce
functools.reduce() applies a cumulative operation to the digits. Although some may prefer this approach, it adds unnecessary function call overhead, making it the least efficient method for this problem.
Python
from functools import reduce
n = 12345
sum = reduce(lambda x, y: x + int(y), str(n), 0)
print(sum)
Explanation: This converts n to a string, then applies a lambda function that iteratively adds each digit (converted to an integer) to an accumulator, starting from 0.
Similar Reads
Sum of number digits in List in Python Our goal is to calculate the sum of digits for each number in a list in Python. This can be done by iterating through each number, converting it to a string, and summing its digits individually. We can achieve this using Pythonâs built-in functions like sum(), map(), and list comprehensions. For exa
2 min read
Python - Sort list of numbers by sum of their digits Sorting a list of numbers by the sum of their digits involves ordering the numbers based on the sum of each individual digit within the number. This approach helps prioritize numbers with smaller or larger digit sums, depending on the use case.Using sorted() with a Lambda Functionsorted() function w
2 min read
Python Program to Get Sum of N Armstrong Number Given a number N, determine the sum of the first N Armstrong numbers using Python. Example: Input : 11Output : 568First 11 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, lies to, 370Their summation is 578Method 1: Using Iterative methodsCreate a while loop that breaks when the desired number of Ar
3 min read
Python - Average of digit greater than K Given elements list, extract elements whose average of digit is greater than K. Input : test_list = [633, 719, 8382, 119, 327], K = 5 Output : [719, 8382] Explanation : (7 + 1 + 9) / 3 = 5.6 and (8 + 3 + 8 + 2) / 4 = 5.2 , both of which are greater than 5, hence returned. Input : test_list = [633, 7
5 min read
Python Program to Find Sum of First and Last Digit Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N. Examples: Input: N = 1247 Output: 8 Explanation: First digit is 1 and Last digit is 7. So, addition of these two (1 + 7) is equal to 8.Input: N = 73
5 min read