Python Program to Reverse a number

Here we will write a program in Python to Reverse a number using while loop. Lets start with the steps:

Step-wise Solution:

1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in another variable.
3. Print the reverse of the number.
4. Exit.

Now lets write the program for the same.

Python Program to Reverse a number:

#Program to reverse a given number

n=int(input("Enter number: "))
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
print("Reverse of the number:",rev)

OUTPUT:

Enter number: 124
Reverse of the number: 421

Comment for any suggestion or concerns.

Leave a Comment