Articles

Write a program to reverse a given number in python

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:

program to reverse a given number in python

 


Basic Python Programs
Python Numbers Program

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Nov 24,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!