codescracker


python

Python Program to Check Reverse Equals Original or Not



« Previous Program Next Program »


Check Reverse Equals Original in Python

To check whether the original number is equal to its reverse or not in python, you have to ask from user to enter the number and start reversing that number. After find the reverse of the given number, check whether the reverse is equal to the given number or not as shown in the program given below.

Python Programming Code to Check Reverse Equals Original

Following python program ask from user to enter any number to check whether it is equal to its reverse or not:

# Python Program - Check Original Equals Reverse or Not

print("Enter 'x' for exit.");
num = input("Enter any number: ");
if num == 'x':
    exit();
try:
    number = int(num);
except ValueError:
    print("Please, enter a number...");
else:
    orig = number;
    rev = 0;
    while number > 0:
    	rev = (rev*10) + number%10;
    	number //= 10;
    if orig == rev:
    	print("Original number is equal to its reverse.");
    else:
    	print("Original number is not equal to its reverse.");

Here is the sample run of the above Python program demonstrating how to check whether original number is equal to its reverse or not:

First enter any number to check it out as shown in the following screenshot of sample initial output of the above program:

check reverse equal original python

Now type any number say 202 which is equal to its reverse, that is 202, therefore after pressing enter you will see the following output:

check original equal original python

Now let's re-run and check the same program for 213 which is not equal to its reverse:

check original equal reverse or not

Here is the same program written and run directly on python shell:

python check reverse equals original

Program Video with Compile and Run

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test