You are currently viewing How to write Python program to reverse a number using For Loop?

How to write Python program to reverse a number using For Loop?

In this article, we will use len() function and For Loop of Python to reverse a number. We will take input from the user and then will print the reverse of that number.

For example, let us suppose the user entered 123 than we have to reverse the number to 321.

In the below example we are using len() function to count the length of the entered value. So that we can run the loop till the length.

Code:

number=input("Enter the number")    //Taking input from user
reverse=0
for i in range(len(number)):
lastvalue = int(number) % 10
reverse = (reverse * 10) + lastvalue
number = int(number) / 10
print(reverse)

Output:

Try it your self with bigger numbers and see the result.

Leave a Reply