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 program allows the user to enter any positive integer. Then it divides the given number into individuals and adds those individual (Sum) digits using 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 allows the user to enter any positive integer, and then that value is assigned to a 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 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 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.

# 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 sum of digits of a number program, When the compiler reaches to sodCalc(val) line, the compiler immediately jumps to the 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 program, When the compiler reaches to Sum= calcSOD(Num) line, then it immediately jumps to the 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, it terminates after completing the first line.

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, placing a condition before using the function recursively is very important. Otherwise, we end up in infinite execution (Same as infinite Loop).