Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Python program to sum all the digits of an input number

Python program to sum all the digits of an input number

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 :

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Do you have any Query / Suggestion