Python Program to find the Reverse of a given number

May 19, 2020

Find the Reverse of a given number

In this Python program we are finding the reverses of a number entered by a user and then print it. For finding the reverse of a number, we have to use a variable and initialize it. Run while loop until condition gets false. and then finally print the reverse of a number as an output. For example: Enter the Number: 1568 Reverse of a Number: 8651

Working:

Step 1. Take the user number for variable num.

Step 2. Take a temp variable and initialized with num value.

Step 3. Take reverse variable and initialized with 0.

Step 4. Run a while until num > 0.

Step 5. Then inside while loop, Obtain remainder.

Step 6. Multiply the reverse variable with 10 and add the obtained remainder to it.

Step 7. Now, divide num by 10 and stored it again in num variable.

Step 8. Repeat Step 5,6,7 until while loop condition gets false.

Step 9, Now print the reverse number as output.

Step 10. Stop.

Python Program:

num = int(input("Enter the Number:"))
temp = num
reverse = 0
while num > 0:
remainder = num % 10
reverse = (reverse * 10) + remainder
num = num // 10
print("The Given number is {} and Reverse is {}".format(temp, reverse))
# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student)

Output:

Enter the Number:1568 The Given number is 1568 and Reverse is 8651

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

#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=””)

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