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

31 comments on “Sum of Digits of a Number in Python”


  • pl

    num=input(“Enter the number:”)
    li=list(num)
    li1=list(map(int,li))
    sum=0
    for i in range(len(li1)):
    sum=sum+li1[i]
    print(“sum of the digits:{}”.format(sum))


  • Tanmay

    n=int(input(“enter the number”))
    sum1=0
    while(n>0):
    a=n%10
    sum1+=a
    n=n//10
    print(sum1)


  • DNYANESHWARI

    n = int(input())
    s = []
    for i in range(a):
    x = int(input())
    s.append(x)
    for i in s:
    count = count + i
    print(count)


  • Anirudh

    #simple
    num =str(input())
    print(“The number is “,num)
    sum = 0
    for i in num:
    sum = sum + int(i)
    print(“The sum of digits is = “,+sum)


  • Uzaif

    #SUM OF DIGITS OF GIVEN NUMBER
    n=int(input())
    n=list(map(int,str(n)))
    print(sum(n))


  • rohitnamdev876

    from array import*
    arr = array(‘i’,[])
    n=int(input(“Enter the number of elements: “))
    for i in range(n):
    arr.append(int(input(“Enter the number: “)))
    total =(sum(arr))
    print(“The sum of these numbers are “,total)


  • Md

    n=12345
    sum=0
    while n>0:
    temp=n%10
    sum=sum+temp
    n=n//10
    print(sum)