We can reverse a number in python using different methods. To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero.
To remove the last digit the modulus operator (%) will be helpful for us, and to remove the last digit division operator (/) can be used. To add the last digit first we need to multiply the previous value by 10. Once you get the logic, you can write the program in any language.
Example of the reverse a number:-
Reverse number: 54321
Procedure to find reverse of a number,
- Take a number
- Declare a temporary variable and initialize it with 0
- Find the last digit of the number
- Multiply the temporary variable by 10
- Add that last digit to the temporary variable
- Remove the last digit of the number.
- Repeat this process from 3 to 6 until the number becomes 0.
How to Reverse a Number in Python
This python program using the while loop to reverse a number. We will take integer numbers while declaring the variables. Then, find the reverse of a number using the while loop and finally, the result will be displayed on the screen.
# Python program to reverse a number # take inputs num = int(input('Enter an integer number: ')) # calculate reverse of number reverse = 0 while(num > 0): last_digit = num % 10 reverse = reverse * 10 + last_digit num = num // 10 # display result print('The reverse number is = ', reverse)
Output for the different input values:-
Enter an integer number: 12345
The reverse number is = 54321
Enter an integer number: 35987642
The reverse number is = 24678953
Reverse Integer using Recursion
We can also use the recursion technique to reverse a number in Python. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.
# Python program to reverse a number using recursion reverse, base = 0, 1 def findReverse(num): global reverse #function definition global base #function definition if(num > 0): findReverse((int)(num/10)) reverse += (num % 10) * base base *= 10 return reverse # take inputs num = int(input('Enter an integer number: ')) # display result print('The reverse number is =', findReverse(num))
Enter an integer number: 4987456
The reverse number is = 6547894
Reverse a Number in Python using Slicing
We read a number and reverse a number using slice operations. We will convert the integer number to string using str() and then, calculate the reverse of a number using the slicing operation.
Syntax of slicing operation:- str(num) [::-1]
# Python program to reverse a number using slicing # take inputs num = int(input('Enter an integer number: ')) # calculate reverse of number reverse = int(str(num)[::-1]) # display result print('The reverse number is = ', reverse)
Enter an integer number: 123456789
The reverse number is = 987654321
Also See:- Find Largest of 3 Numbers in Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!