Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 2 - Elementary Programming - Programming Exercises - Page 58: 2.13

Answer

Program: #prompt the user for a number number = eval(input("Enter a 4-digit integer: ")) #get the individual digits from the number onesDigit $=$ number $\% 10$ remainingNumber = number // 10 tensDigit = remainingNumber $\% 10$ remainingNumber = remainingNumber // 10 hundreadsDigit = remainingNumber $\% 10$ remainingNumber = remainingNumber // 10 thousandsDigit = remainingNumber \% 10 # Display integer in reverse order print( thousandsDigit) print( hundreadsDigit) print( tensDigit) print( onesDigit)

Work Step by Step

Step 1 of 3: Program Plan: Create a python file with name 2_13PE.PY. In the file, - Get input from user. It is the a 4-digit number. - Get the individual digits from the number using integer division (//) and remainder (\%) operators. - Display the individual digits in reverse order. Step 2 of 3: Program: #prompt the user for a number number = eval(input("Enter a 4-digit integer: ")) #get the individual digits from the number onesDigit $=$ number $\% 10$ remainingNumber = number // 10 tensDigit = remainingNumber $\% 10$ remainingNumber = remainingNumber // 10 hundreadsDigit = remainingNumber $\% 10$ remainingNumber = remainingNumber // 10 thousandsDigit = remainingNumber \% 10 # Display integer in reverse order print( thousandsDigit) print( hundreadsDigit) print( tensDigit) print( onesDigit) Sample Output: Enter a 4-digit integer: 3125 3 1 2 5
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.