In this tutorial, we are going to learn how to sum all the digits of an input number using a Python program, this kind of simple problem can be asked in CBSE class XI / XII “Computer Science with Python” Examination.
Environment used :
- OS : Linux
- Python Version : Python 3.6.9
- Text Editor : Atom ( you can use Notepad, Notepad++, Kate , Geany, IDLE …)
Lets move to our program how to sum all the digits of an input number using a Python , for this we have to create a file named addDigit.py with code given below, now open terminal /cmd, use CD command to go there and run
for Linux : python3 addDigit.py
for Windows user : python addDigit.py
# Programe : how to sum all the digits of an input number # Created by Sanjay Prasad no = int(input("Enter a Positive Number: ")) result = 0 noORG = no while no > 0: rem = no % 10 result = result + rem no = int(no/10) print("Sum of all digits of no. ", noORG, "is: ", result)
Screen shot of Out put :