Python Program to find Sum of Digits

You are Here:

Find Sum of Digits

In the following example, we will add all the digits in the given number (12345) for just one time.

For example, 12345 = 1 + 2 + 3 + 4 + 5
i.e., 12345 = 15

Example

Python Compiler
import math num = 12; copyNum = num; total = 0; #Add Each digit from last digit. while(copyNum != 0): total += copyNum % 10 copyNum = math.floor(copyNum / 10) print("The sum of digit %d: %d" % (num, total))

Output

The sum of digit 12: 3

Find Sum of any given Digits

In the following example, we will find the sum of any given digits.

Example

Python Compiler
import math num = int(input("Enter a (int) number: ")); copyNum = num; total = 0; #Add Each digit from last digit. while(copyNum != 0): total += copyNum % 10 copyNum = math.floor(copyNum / 10) print("The sum of digit %d: %d" % (num, total))

Output

Enter a (int) number: 14 The sum of digit 14: 5

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.

We are working to cover every Single Concept in Python.