Python Program to Add Digits of a Number

In this article, you will learn and get code in Python, to find and print the sum of digits of a number entered by user at run-time. Here are the list of approaches used to do the task:

For example, if user enters a number say 235, then the output will be 2+3+5, that is 10.

Add Digits of a Number using while Loop

The program given below receives a number as input from user, and uses a while loop to find the sum of digits of given number:

print("Enter a Number")
num = int(input())
sum = 0
while num>0:
    rem = num%10
    sum = sum+rem
    num = int(num/10)
print("\nSum of Digits of Given Number: ", sum)

Here is the initial output produced by this Python program:

add digits of number python

Now supply the input say 123 and press ENTER key to find and print the summation of its digit 1, 2, and 3 as shown in the snapshot given below:

add number digits python

The dry run of above program with user input 123 goes like:

Modified Version of Previous Program

This is the modified version of previous program. In this program, if user enters a number say 124, then output looks like 1+2+4=7

print(end="Enter a Number: ")
num = int(input())

sum = 0
print(end="\n")
while num>0:
    rem = num%10
    sum = sum+rem
    num = int(num/10)
    if num==0:
        print(end=str(rem))
    else:
        print(end=str(rem)+ "+")

print(" = " +str(sum))

Here is its sample run with user input, 130259:

python add digits of number

Note - The end is used to skip adding an automatic newline using print().

Add Digits of Number using for Loop

Now this program uses for loop instead of while to do the same task, that is to find and print the sum of given number's digits.

print("Enter a Number: ", end="")
num = int(input())

sum = 0
temp = num

for i in range(len(str(temp)), 0, -1):
    rem = num%10
    sum = sum+rem
    num = int(num/10)

print("\nSum of Digits of " +str(temp)+ " = " +str(sum))

Here is its sample run with user input, 4052:

python find sum of digits of number

Note - The third parameter of for loop (-1) is used to loop with loop variable (i) in reverse order. That is, the size of given number to 1 (one greater than 0 (the second parameter))

Since the length of "4052" (a string) is 4, therefore all three statements gets executed three times.

Note - The str() is used to convert an int value to a string type value

Note - The len() is used to find the length of string.

Add Digits of Number using Function

This program uses a user-defined function named addNumDig() to find the sum of digits of a given number. The function receives a number as its argument and returns the sum of its digit.

def addNumDig(n):
    sum = 0
    while n>0:
        rem = n%10
        sum = sum+rem
        n = int(n/10)
    return sum

print("Enter a Number: ", end="")
num = int(input())

res = addNumDig(num)
print("\nSum of Digits of " +str(num)+ " = " +str(res))

This program produces the same output as of previous program.

Add Digits of Number using Class

This is the last program of this article. The program is created using class and object, an object-oriented feature of Python.

class fresherearth:
    def addNumDig(self, n):
        sum = 0
        while n>0:
            rem = n%10
            sum = sum+rem
            n = int(n/10)
        return sum

print("Enter a Number: ", end="")
num = int(input())

ccObj = fresherearth()
res = ccObj.addNumDig(num)

print("\nSum of Digits of " +str(num)+ " = " +str(res))

Here is its sample run with user input, 1046:

add number digits using class python

Using the following statement:

ccObj = fresherearth()

All properties of the class named fresherearth gets assigned to an object named ccObj. Now this object can be used to access the member function of class fresherearth using dot (.) operator.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »