61.4k views
19 votes
Python exercise grade 10

Write a program that displays a number with its digit reversed. Generalize the program so that
the number can be one, two, or three digits. The program stops when users enters 0
Enter a number: 123
The reversed digit is: 321
Enter a number: 1
The reversed digit is: 1
Enter a number: 34
The reversed digit is: 43
Enter a number: 0

User Lorinne
by
4.1k points

1 Answer

2 votes

while True:

num = int(input("Enter a number: "))

if num == 0:

break

first = num // 100

second = (num // 10) - (first * 10)

third = (num - (first * 100)) - (second * 10)

reversed_digit = (third * 100) + (second * 10) + first

if 9 < num < 100:

reversed_digit = reversed_digit // 10

elif num < 10:

reversed_digit = reversed_digit//100

print("The reversed digit is:",reversed_digit)

I wrote my code in python 3.8. I hope this helps.

User Edward M Smith
by
4.2k points