In this article we will show you, how to write a Python Program to Find 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 sum of digits in python program allows the user to enter any positive integer and then it will divide the given number into individual digits and add those individual (Sum) digits using Python While Loop.

**PYTHON CODE**

# 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)

**OUTPUT**

**ANALYSIS**

This program for sum of digits in python allows the user to enter any positive integer and then, that number is assigned to variable Number.

Next, Condition in the While loop will make 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 Iteration the values of both Number and Sum has been 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**

From the Third Iteration of this sum of digits in python program, the values of both Number and Sum has been changed as:

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**

From the Fourth Iteration the values of both Number and Sum has been changed as:

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 will fail

Last print statement will print the Sum variable as output. So, the output of given variable 4567 is:

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

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

**PYTHON CODE**

# 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)

**OUTPUT:**

**ANALYSIS:**

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

def Sum_Of_Digits(Number):

We already explained LOGIC above example. Please refer Python Program to Find Sum of the Digits of a Given Number using While Loop Analysis section.

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

This program to find sum of digits in python allows the user to enter any positive integer and then it will divide the given number into individual digits and add those individual (Sum) digits by calling the function recursively. Please refer Python Recursion for further reference

**PYTHON CODE**

# 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)

**OUTPUT**

**ANALYSIS**

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

def Sum_Of_Digits(Number):

In this function, below statement will help to call the function Recursively with updated value. If you miss this statement then, after completing the first line it will terminate.

Sum_Of_Digits(Number //10)

For this Python sum of digits of a number program example, Number = 4567 will return the output as 7.

Lets see the Python If Statement,

if (Number > 0) will 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 will end up in infinite execution (Same like infinite Loop).

Please be careful :)

Thank you for Visiting Our Blog