Python Program to Find Sum of Digits of a Number

In this section, we discuss how to write a Python Program to Find the Sum of Digits of a Number using While Loop, Functions, and Recursion.

Python Program to Find Sum of Digits of a Number using While Loop

This Python sum of digits program allows the user to enter any positive integer. Then it divides the given number into individuals and adds those individual (Sum) digits using Python While Loop.

Number = int(input("Please Enter any Number: "))
Sum = 0

while(Number > 0):
    Reminder = Number % 10
    Sum = Sum + Reminder
    Number = Number //10

print("\n Sum of the digits of Given Number = %d" %Sum)
Python Program to find Sum of Digits of a Number using While Loop

This program for the sum of digits in python allows the user to enter any positive integer and then, that value assigned to variable Number. Next, Condition in the Python While Loop makes sure that the given number is greater than 0 (Means Positive integer and greater than 0).

User Entered value for this program : Number = 4567 and Sum = 0

First Iteration

Reminder = Number%10
Reminder = 4567 % 10 = 7

Sum= Sum+ Reminder
Sum= 0 + 7 = 7

Number = Number/10
Number= 4567 / 10 = 456

Python Program to Find Sum of Digits of a Number Second Iteration:

From the first Python Iteration, Number= 456 and Sum= 7

Reminder = 456 % 10 = 6

Sum= 7 + 6 = 13

Number= 456 / 10 = 45

Third Iteration: For the Third Iteration, the values of Number= 45 and Sum= 13

Reminder = 45 % 10 = 5

Sum= 13 + 5 = 18

Number= 45 / 10 = 4

Fourth Iteration: For the Fourth Iteration, Number= 4 and Sum= 18

Reminder= 4 % 10 = 4

Sum= 18 + 4 = 22

Number= 4 / 10 = 0

Here Number= 0. So, the while loop condition fails.

Last print statement prints the variable as the output. So, the Python Program to Find Sum of Digits of a Number output of the given variable 4567 is:

Python Program to Find Sum of Digits of a Number Using Functions

This sum of digits in the python program allows the user to enter any positive integer. Then it divides the given number into individual digits and adds those individual (Sum) digits using Functions.

# Python Program to find Sum of Digits of a Number using Functions

def sodCalc(val):
    total = 0
    while(val > 0):
        Reminder = val % 10
        total = total + Reminder
        val = val //10
    return total

val = int(input("Please Enter any Value: "))
total = sodCalc(val)
print("\n Sum of the digits of Given Value = %d" %total)
Please Enter any Value: 12345

 Sum of the digits of Given Value = 15

In this Python sum of digits of a number program, When the compiler reaches to sodCalc(val) line, the compiler immediately jumps to below function:

def sodCalc(val):

Python Program to Find Sum of Digits of a Number using Recursion

This program to find the sum of digits allows the user to enter any positive integer. Then it divides the given integer into individual digits and adds those individual (Sum) digits by calling the function recursively.

tot = 0
def calcSOD(Num):
    global tot
    if(Num > 0):
        Reminder = Num % 10
        tot = tot + Reminder
        calcSOD(Num //10)
    return tot

Num = int(input("Please Enter any Value: "))
tot = calcSOD(Num)
print("\n Sum of the digits = %d" %tot)
Please Enter any Value: 456

 Sum of the digits = 15

Within this Python sum of digits of a number program, When the compiler reaches to Sum= calcSOD(Num) line then it immediately jump to below function:

calcSOD(Num):

In this function, the below statement helps to call the function Recursively with the updated value. If you miss this statement, after completing the first line, it terminates.

calcSOD(Num //10)

For this Python Program to Find the Sum of Digits of a Number example, Number= 4567 returns the output as 7.

Let’s see the If Statement,

if (Num > 0), check whether the num is greater than 0 or not. For Recursive functions, it is very important to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like infinite Loop).