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.

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our Facebook page, follows us on Twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply