Reverse the Five Digit Number in Python


If a five-digit number is input through the keyboard, write a program to reverse the number.

The program is a simple program that takes an input of a five-digit number from the user and then reverses the digits of the number. The program uses the modulus operator(%) and the integer division operator(//) to isolate each digit of the input number and then multiplies each digit by a power of 10 to create the reversed number. The final reversed number is then printed as the output.

It starts by taking an input of a five-digit number and initializing a variable "revnum" to zero. Then it uses the modulus operator(%) and the integer division operator(//) to isolate each digit of the input number one by one and stores it in the variable 'a'. Then it multiplies the digit by a power of 10 to create the reversed number. The final reversed number is then printed as the output. It also can be refactored to a more efficient solution using string slicing and concatenation, which is more pythonic.


Source Code

n=int(input("Enter the five digits Number:"))
revnum=0
a=n%10
n=n//10
revnum=revnum+a*10000
 
a=n%10
n=n//10
revnum=revnum+a*1000
 
a=n%10
n=n//10
revnum=revnum+a*100
 
a=n%10
n=n//10
revnum=revnum+a*10
 
a=n%10
revnum=revnum+a
 
print("Reverse Five digits =",revnum)

Output

Enter the five digits Number:23472
Reverse Five digits = 27432


Example Programs