Dark Mode On/Off
PUBLISHED ON: JULY 6, 2021

# Python Program to find sum of digits

In this tutorial, we will learn how to calculate the sum of all the digits of a given number. We will learn all the possible methods to do this program. We will be using recursive functions, loops, and type conversion for this program in Python.

In type conversion, we will convert integer to string for getting each digit of the number. Before moving forward you must know what is a string. Check out this article to know about String in Python.

The program should take an integer number as an input from the user and print the sum of its digits. For example,

Input- 4321

Output- 10

Input- 821

Output- 11

Here are three approaches, you can use to print the sum of digits.

1. Using str() and int()
2. Using iteration
3. Using recursion

## Python Program 1: Using str() and int() method

Before starting with the program, let's first know about str() and int() methods. They are built-in functions in the Python Library.

The str() method converts and returns the object to string and the int() method converts the object to an integer.

### Algorithm

Step 1- Take input from the user

Step 2- Declare a variable for storing the sum

Step 3- Convert number to string

Step 4- Run loop for each digit of a number

Step 5- Convert digit to integer and add it to sum

Step 6- Print the sum.

## Python Program

Look at the program to understand the implementation of the above-mentioned approach.

``````n=int(input("Enter number"))

sum=0

for digit in str(n):
sum=sum+int(digit)

print("Sum of digits",sum)``````

Enter number23451
Sum of digits 15

## Python Program 2: Using iteration

In this program, we will use looping statements for calculating the sum. Loops are used to execute a particular piece of code repeatedly. For loop, while, and do-while is some looping statements.

For getting the rightmost digit of a number divide it by 10 till the number becomes 0. The remainder at last will be the rightmost digit. To get the reminder to use the remainder operator "%". For getting all the digits of a number divide the quotient obtained by 10. To get an integer quotient every time use "//".

### Algorithm

Step 1- Define a function Sum with parameter n

Step 2- Declare variable sum to store the sum of digits

Step 3- Define a loop that will run till n is not 0

Step 4- Add the sum variable to the remainder returned by (n%10)

Step 5- Update n to n//10

Step 6- Take user input

Step 7- Call function Sum and pass input as a parameter

Step 8- Print the value returned by Sum.

## Python Program

Look at the program to understand the implementation of the above-mentioned approach.

``````def Sum(n):
sum = 0
while (n != 0):
sum = sum + (n % 10)
n = n//10
return sum

n=int(input("Enter number"))
print("Sum of digits",Sum(n))``````

Enter number342
Sum of digits 9

## Python Program 3: Using recursion

The above method can also be executed by defining a recursive function. Recursive functions are the ones that call themselves inside the function definition. Using recursion will eliminate the need for loops in the code.

Follow the algorithm for a detailed explanation of the working of the program.

### Algorithm

Step 1- Define a function sum_of_digits with parameter n for calculating the sum

Step 2- Check if n is less than 10, if true return n

Step 3- Else, divide the number by 10 and calculate the remainder (n%10)

Step 4- Call the function recursively and pass (n//10) as a parameter

Step 5- Return sum of the remainder and the value returned by the function

Step 6- Take input from the user

Step 7- Call function sum_of_digits and pass input as a parameter

## Python Program

Look at the program to understand the implementation of the above-mentioned approach.

``````def sum_of_digit(n):
if n< 10:
return n
else:
return n%10 + sum_of_digit(n//10)

number = int(input("Enter number: "))
# Function call and output display
print("Sum of digit is",sum_of_digit(number))``````

Enter number: 5421
Sum of digit is 12

## Conclusion

We have learned three different ways by which we can calculate the sum of digits of a number in Python. We can use methods of the str class in Python like str() and int() for converting an integer to string and vice-versa.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.