Sum of Digits of a Number in Python
Find the sum of the Digits of a Number in Python
Given an input the objective to find the Sum of Digits of a Number in Python. To do so we’ll first extract the last element of the number and then keep shortening the number itself.
Example
Input : number = 123
Output : 6
Find the sum of the Digits of a Number
Given a number as an input the objective is to calculate the sum of the digits of the number. We first break down the number into digits and then add all the digits to the sum variable. We use the following operators to break down the string,
- Modulo operator %: We use this operator to extract the digits from the number.
- Divide operator /: We use this operator to shorten the number after the digit has been extracted.
We use the above-mentioned operators to find the sum of the digits of the number. Here are some of the methods to solve the above-mentioned problem.
- Method 0: Using String Extraction method
- Method 1: Using Brute Force
- Method 2: Using Recursion I
- Method 3: Using Recursion II
- Method 4: Using ASCII table
- Method 5: Using map(), sum() and strip methods
- Method 6: One Line recursive function
- Method 7 : The cool method
We’ll discuss the above-mentioned methods in the upcoming sections.
Method 0: Using String Character Extraction
We will extract each character in the string input and convert it to an individual character’s integer equivalent.
num = input("Enter Number: ") sum = 0 for i in num: sum = sum + int(i) print(sum)
Output
Enter Number: 12345 15
Method 1: Using Brute Force
We extract each digit here by finding the modulus of the whole input by 10.
Python Code
num = 12345 sum = 0 while num!=0: digit = int(num%10) sum += digit num = num/10 print(sum)
Output
15
Method 2: Using Recursion I
Python Code
num, sum = 12345, 0 def findSum(num, sum): if num == 0: return sum digit = int(num % 10) sum += digit return findSum(num / 10, sum) print(findSum(num, sum))
Output
15
Method 3: Using Recursion II
Python Code
num = 12345 def findSum(num): if num == 0: return 0 return int(num % 10) + findSum(num / 10) print(findSum(num))
Output
15
Method 4: Using ASCII Table
Please check the ASCII Table here
Python Code
num, sum = 12345, 0 for i in range(len(str(num))): # ord methods helps with ASCII sum += ord(str(num)[i]) - 48 print(sum)
Output
15
Method 5: Using map(), sum() and strip methods
Explain given in the code itself below –
Python Code
def getSum(n): # convert into string num_string = str(n) # fetch each individual char using strip method # find comparable int and store it in map # covert it into list list_of_number = list(map(int, num_string.strip())) print(list_of_number) # sum function returns the sum of all items in list return sum(list_of_number) n = int(input("Enter the number: ")) print(getSum(n))
Output
Enter the number: 12345 15
Method 6: One Line recursive function
Let’s look at the function below –
def sumDigits(n): return 0 if n == 0 else int(n % 10) + sumDigits(int(n / 10)) # Driver code n = int(input("Enter the number: ")) print(sumDigits(n))
Output
Enter the number: 12345 15
Method 7: The cool method
We kind of have discussed this method above. But its amalgamation of different python tricks –
n = [int(d) for d in input("Enter the number : ")] print("the sum of digits is : ", sum(n))
Output
Enter the number: 12345 15
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
Even more simpler
num = (input(“Enter your number: “))
con =[ ]
for i in num:
con.append(int(i))
print(sum(con))
data = int(input(“enter the num: “))*10
sum = 0
for i in range(len(str(data))):
sum += i
print(sum)
number = input(“Enter the number : “)
######## METHOD – 1
sums = 0
for i in number:
sums = sums + int(i)
print(sums)
from functools import reduce
x= int(input(“Enter a number\n”))
y= int(input(“Enter a number\n”))
R = list(range(x,y+1))
sum = reduce(lambda x,y : x+y , R)
print(sum)
#sum of digits of a number
sod=0
num=int((“Enter the number”))
while(num>0)
last=num%10
sod=sod+last
rem=num//10
print(“The sum of digits is:”sod)
n = [int(d) for d in input(‘Enter the number : ‘)]
print(‘Sum of digits of a number : ‘,sum(n))
n = [int(d) for d in input(‘Enter the number : ‘)]
print(‘the sum of digits is : ‘,sum(n))