Reverse a Number in Python
Find the Reverse of a Number in Python
Given an integer input the objective is to reverse the given integer number using loops and slicing. Therefore, we’ll write a program to Find the Reverse of a Number in Python Language.
Example Input : 123 Output : 321
Find the Reverse of a Number in Python Language
We need to write a python code to reverse the given integer and print the integer. The typical method is to use modulo and divide operators to break down the number and reassemble again in the reverse order. Here are some of the methods to solve the above mentioned problems,
- Method 1: Using Simple Iteration
- Method 2: Using String Slicing
- Method 3: Using Recursion
Let’s go through the above mentioned methods in the sections below.
Method 1: Using Simple Iteration
Working
In this method we’ll use the concept of loops to repeat the process of breaking down the number and adding it back in the reverse order.
For a given integer variable number we perform the following,
- Run a while loop with the condition number >0.
- Extract the last digit as Remainder using Modulo operator.
- Using the formula reverse = ( reverse * 10 ) + remainder , we keep changing the reverse value.
- Break down the Nunber using divide operator.
- Print the reverse variable.
Let’s implement the above mentioned Logic in Python Language.
Python Code
num = 1234 temp = num reverse = 0 while num > 0: remainder = num % 10 reverse = (reverse * 10) + remainder num = num // 10 print(reverse)
Output
4321
Method 2: Using String Slicing
Working
In this method we’ll use the string slicing methods to reverse the number. First we’ll convert the number into a string and then using the string slicing property in python language, we’ll slice the string to break it down and add it up in reverse order.
For a given integer input, we perform the following operations,
- Convert the number into string format.
- Reverse the number using string slicing.
Let’s implement the above mentioned logic in Python Language.
Python Code
num = 1234 print(str(num)[::-1])
Output
4321
Method 3: Using Recursion
Working
In this method we’ll use recursion to perform all the operations we perform in method 1. We’ll use the same formula but instead of iterating through a loop, we’ll use recursion. To know more about recursion, check out our page Recursion in Python.
Given an integer input number, we perform the following,
- Define a recursive function recursum() that takes in number and reverse variable as arguments.
- Set the base case as number == 0 and step recursive call as recursum(num/10, reverse).
- Print the returned value using print() function.
Let’s implement the above mentioned logic in Python Language.
Python Code
def recursum(number,reverse): if number==0: return reverse remainder = int(number%10) reverse = (reverse*10)+remainder return recursum(int(number/10),reverse) num = 1234 reverse = 0 print(recursum(num,reverse))
Output
4321
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
shortest way to reverse.
n = input()
reverse = n[::-1]
print(reverse)
num = [str(d) for d in input(“Enter the Number:”)]
s=int(”.join(num[::-1]))
print(‘Reverse order is:’,s)
print(int(input())[::-1])
#reverse of a number
n = int(input())
print(str(n)[::-1])
a=input()
n=len(a)
for i in range(n-1,-1,-1):
print(int(a[i]),end=””)
print(str(int(input()))[::-1])
n=input(‘enter number’)
sum=”
for i in n:
sum=i+sum
print(sum)
one line code:
print(input()[::-1])
Correct one……
num = (input(“Enter the number: “))
for i in str(num[::-1]):
print(i, end=””)
Thanks, Himanshu for contributing your code
num = input(“Enter the number: “)
for i in str(num[::-1]):
print(i, end= ”)
num = (input(“Enter the number: “))
lst = []
for i in str(num[::-1]):
lst.append(i)
print(i, end=””)
we can also implement this using list ,list version is very short ?
li=list(input(“Enter the No.”))
print(“Reverse of this no. is “,””.join(li[::-1]))
thanks Ashish, we’ll surely consider this approach. Or if you want you can also comment the code, and we will post it on this page
so this my approach for reversing the list
#taking input and converting it as list
li=list(input(“Enter the No.”))
#now using string join method we can join the list and this code li[::-1] will reverse the list this method is called reversing list by slicing method
print(“Reverse of this no. is: ” , “”.join(li[::-1]))
Thanks for contributing the code Ashish
What is use of temp variable in this program
We have used temp here, for creating a copy of the user inputed number, so that in the end we can print both the original number and the reversed number