Digit Addition: How to add Digits of a given integer in Python?
In this Python tutorial, we will be working on digit addition. That means we will learn how to add digits of a number in Python with an easy example. We hope this is going to be very much easy to understand as an easy example is taken with input and output.
DIGIT ADDITION in Python
Digit Addition: There’s a number of two or more digits. Now the task is to add the digits of the number. And the outcome should be the sum of the digits.
Suppose your number is 1542. Then the output should be 12. ( Because of 1+5+4+2=12 )
Let’s look at the below Python program to add the digits of a number
PROGRAM: Summation of digits of a number in Python
n=int(input("Input a number of two or more digits:")) check=sum=0 while(check==0): i=n%10 #taking out the last digit n=(n-i)/10 #update the original input by removing the last digit n=round(n) sum=sum+i #Digit addition if(n<10): sum=sum+n break print("Sum of the digits is:",sum) #print output
Input a number of two or more digits:2457 Sum of the digits is: 18
Input a number of two or more digits:689 Sum of the digits is: 23
This is a simple problem, someone can modify this problem like the sum of distinct digits of a number. I leave this to you to find out.
- Clockwise & CounterClockwise Rotation of Matrix using Numpy in Python3
- How to detect strings that contain only whitespaces in Python
- How to remove null values from the list in Python