Easiest Way to Reverse A Number in Python

In this Python article, we will cover the program by which we can reverse any given number. Let’s begin.

1. Reversing a Number in Python

Any given number can be reversed using different techniques. The reversing of the number means that the last digit will come to the first place, the second last number will come to the second position, and so on.

Reverse a number in Python
Example:
Input : 12345
Output : 54321
Algorithm:
Input: Number 

(1) Initialize reverse = 0 
(2) Loop while Number > 0      
  (a) Multiply reverse by 10 and add remainder of Number 
         Divide by 10 to reverse                
             reverse = reverse*10 + Number%10;      
  (b) Divide Number by 10 
(3) Return reverse

Ouput: Reversed Number

Some of the topics which will be helpful for understanding the program implementation better are:

Let’s go through each of the implementation methods one by one.


2. Python Program to Reverse the Number

2.1. Using the While Loop

The input from the user has been taken and the while loop has been used to perform the operation.

# Python program to Reverese Number in Simplest way
number = int(input("Enter the number to Reverse : "))
reverse = 0
 
while(number > 0):
  temp = number % 10
  reverse = reverse * 10 + temp
  number = number // 10
 
print("The Reversed Number is: ", reverse)
Output
Enter the number to Reverse : 123452588
The Reversed Number is:  885254321

2.2. Using Recursion and Global Concept

in this method, we will use the recursion and the declared variables are with Global Keyword.

# Python program to Reverese Number using reursion and Global
reverse = 0
base = 1
 
def reverse_the_number(n):
  
  global reverse
  global base
  
  if(n > 0):
    reverse_the_number((int)(n / 10))
    reverse += (n % 10) * base
    base *= 10
    return reverse
 
 
# Main
num = int(input("Enter number to reverese : "))
print("Reversed Number is: ", reverse_the_number(num))
Output
Enter the number to reverse : 12345
Reversed Number is: 54321

2.3. Using String Slicing Method

# Python program to Reverese Number using String Slicing
num = int(input("Enter the number : "))
print("Reversed Number is: ", str(num)[::-1])
Output
Enter the number :21396
Reversed Number is: 69312

3. Conclusion

In this article, we discussed the easiest and best ways to reverse any given number using the functions and different methods like the loop, recursion, and string slicing methods.


Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x