Python Program to Find Sum Of Digits Of a Number

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 in Python

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.

Find the Sum of the Digits in a Number in Python

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.

Run
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

Run
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

Run
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

Run
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

Run

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

Run
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 –

Run
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 –

Run
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

27 comments on “Python Program to Find Sum Of Digits Of a Number”


  • abhishekrajhans5001

    #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)


  • Bharath

    n = [int(d) for d in input(‘Enter the number : ‘)]
    print(‘Sum of digits of a number : ‘,sum(n))


  • Bharath

    n = [int(d) for d in input(‘Enter the number : ‘)]
    print(‘the sum of digits is : ‘,sum(n))