Python Program to Reverse a Given Number
This python program we reverses a given number. Reversing a given number requires integer division and here we use operator // for integer division or floor division.
Original or Input Number: 123456
Reversed or Output Number: 654321
Python Source Code: Reversing Given Number
# Python Program to Reverse Given Number
# Reading Number and Converting to Integer
number = int(input('Enter Number: '))
copy = number
# Set Reverse variable to 0
reverse = 0
# Finding Reverse
while number != 0:
remainder = number%10
reverse = reverse *10 + remainder
number = number//10
# Displaying Reverse
print('Reverse of %d is %d' %(copy, reverse))
Output
Enter Number: 123456 Reverse of 123456 is 654321
Consider understanding these two cases for division to understand above program.
Case 1: Floating Point Division: / Operator
>>> 7/3 2.3333333333333335
Case 2: Floor Division: // Operator
>>> 7//3 2