Python Program to Find Sum of Digits of Given Number

In this article, we will learn about python program to find sum of digits of given number.

Getting Started

The task is to write a function that gets all the digits of number entered by user. Then, sum them and return it. For example,

If user enters 1234, then sum will be –

1 + 2 + 3 + 4 = 10

10 will be returned by function.

We can do so in many ways –

Using str() and for Loop

We can find sum of digits of number in python using str and python for loop as shown below –

Pseudo Algorithm

  • Initialize sum = 0
  • Run a for loop on input value
  • At each iteration, convert each value into integer using int. Then, add it into sum.
  • At end of for loop value of variable sum is sum of all digits of given number.
 
def findSum(n):
    sum = 0
    for i in n:
        sum += int(i)
    return sum

n = input("Enter a number")
print(findSum(n))

Output:

 
Enter a number
5674
22

Using sum() Method

Pseudo Algorithm

  • Our task is to convert input into python list, map() and strip() methods.
  • Then, use built-in method, sum(), to get sum of elements present in list.
 
def findSum(num):
    numList = list(map(int, num.strip()))
    return sum(numList)

num = input("Enter a number")
print(findSum(num))

Output:

 
Enter a number
7768
28

Using Brute Force Approach

Pseudo Algorithm

  • Initialize a variable sum = 0
  • Run a while loop until value of variable num becomes 0.
  • In each iteration, get the last digit of number and add it to sum.
  • Then, divide the num by 10.
  • At the end of while loop, value of sum is our desired result.
 
def findSum(num):
    sum = 0
    while(num != 0):
        sum = sum + num % 10
        num = num // 10
    return sum

num = int(input("Enter a number"))
print(findSum(num))

Output:

 
Enter a number
1234
10

Using Recursion

Pseudo Algorithm

  • In each function call, get the last digit of number.
  • After that, add it to result of same function call with number divided by 10.
  • Break the function call when number becomes 0.
 
def findSum(num):
    if(num == 0):
        return num
    else:
        return (num % 10) + findSum(num // 10)

num = int(input("Enter a number"))
print(findSum(num))

Output:

 
Enter a number
6578
26

Improved Version

 
def findSum(num):
    return 0 if(num == 0) else (num % 10) + findSum(num // 10)

num = int(input("Enter a number"))
print(findSum(num))

Output:

 
Enter a number
1234
10

Using ASCII Values

Pseudo Algorithm

  • Run a for loop for each digit of number.
  • In each iteration, Difference between ASCII value current digit and 0 will give digit value.
  • Then, add it to sum.
  • At the end of for loop, value of variable sum is desired result.
 
def findSum(num):
    sum = 0
    for i in num:
        sum = sum + (ord(i) - ord('0'))
    return sum

num = input("Enter a number")
print(findSum(num))

Output:

 
Enter a number
768
21

One Liner Solution

 
n = [int(d) for d in input("Enter a number")]
print(sum(n))

Output:

 
Enter a number
345
12

That’s how we can write python program to find sum of digits of given number.

Learn about other python examples – https://tutorialwing.com/python-tutorial/

Reference: Official Doc

Leave a Reply