Skip to content
Home » Program to Reverse a Number in Python

Program to Reverse a Number in Python

Learn about Program to Reverse a Number in Python in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Program to Reverse a Number in Python

# Python program to reverse a number

# input from user
num = int(input('Enter a number: '))

# calculate the reverse of number
reverse = 0
while(num > 0):
    last_digit = num % 10
    reverse = reverse * 10 + last_digit
    num = num // 10

# printing result
print('The reverse number is = ', reverse)

Output:

Enter a number: 234
The reverse number is = 432

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Program to Print ASCII Value of Characters in Python