Write a program to reverse a given number in python
In this python programming article, we are going to learn:
write a program to reverse a given number in python
Program to reverse a number in Python
Lets write a program to reverse a number in python using for loop.
The program to reverse a given number in python is as follows:
# Owner : TutorialsInhand Author : Devjeet Roy
number = int(input("Enter number: "))
temp = str(number)
new_num = 0
for i in range(len(temp)):
last_bit = number % 10
new_num = (new_num * 10) + last_bit
number = number // 10
print("The reversed is:",new_num)
The output of the program to reverse a given number in python is as follows:
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py En
Enter number: 12345
The reversed is: 54321
Few important tips about the program
1. We take a user input and make it of integer type. We keep a clone of it in the string format, to evaluate the length of the number.
2. We divide the number by 10, get the remainder and create a new number, multiplying it by 10 and reversing the index positions.
3. Lastly, we reduce the number by dividing it by 10.
Write a program to reverse a given number in python:
Would you like to see your article here on tutorialsinhand.
program by tutorialsinhand.com
About the Author
Full Stack Web Developer & Blockchain Enthusiast
Page Views :
Published Date :