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 individual digits and adds those individual (Sum) digits using Python While Loop.

# Python Program to find Sum of Digits of a Number 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 for the sum of digits in python allows the user to enter any positive integer and then, that number 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 Python sum of digits of a number 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

Second Iteration: From the first Python Iteration, the values of both the Number and Sum changed as Number = 456 and Sum = 7

Reminder = Number %10
Reminder = 456 % 10 = 6

Sum = Sum+ Reminder
Sum = 7 + 6 = 13

Number = Number / 10
Number = 456 / 10 = 45

Third Iteration: For the Third Iteration of this sum of digits in python program, the values of Number = 45 and Sum = 13

Reminder = Number %10
Reminder = 45 % 10 = 5

Sum = Sum+ Reminder
Sum = 13 + 5 = 18

Number = Number / 10
Number = 45 / 10 = 4

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

Reminder = Number %10
Reminder = 4 % 10 = 4

Sum = Sum+ Reminder
Sum = 18 + 4 = 22

Number = Number / 10
Number = 4 / 10 = 0

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

Last print statement prints the Sum 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 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 Sum_Of_Digits(Number):
    Sum = 0
    while(Number > 0):
        Reminder = Number % 10
        Sum = Sum + Reminder
        Number = Number //10
    return Sum

Number = int(input("Please Enter any Number: "))
Sum = Sum_Of_Digits(Number)
print("\n Sum of the digits of Given Number = %d" %Sum)
Please Enter any Number: 12345

 Sum of the digits of Given Number = 15

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

def Sum_Of_Digits(Number):

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

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

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

Sum = 0
def Sum_Of_Digits(Number):
    global Sum
    if(Number > 0):
        Reminder = Number % 10
        Sum = Sum + Reminder
        Sum_Of_Digits(Number //10)
    return Sum

Number = int(input("Please Enter any Number: "))
Sum = Sum_Of_Digits(Number)
print("\n Sum of the digits of Given Number = %d" %Sum)
Please Enter any Number: 456

 Sum of the digits of Given Number = 15

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

def Sum_Of_Digits(Number):

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.

Sum_Of_Digits(Number //10)

For this sum of digits of a number example, Number = 4567 returns the output as 7.

Let’s see the Python If Statement,

if (Number > 0), check whether the number 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).