Programming Tutorials Hub

Learn Python with simple code, clear explanations, and examples

Python program to check whether a given number is Palindrome or not using while loop

📄 Code

number = input("Enter a number:")
number = int(number)
temp = number
sum = 0

while number > 0:
    rem = number % 10
    sum = sum *10 + rem
    number = number//10

if temp == sum:
    print("Palindrome")
else:
    print("Not Palindrome")
        
💡 Output
Enter a number:121
Palindrome

Enter a number:123
Not Palindrome
        
🧠 Code Explanation
Palindrome number is a number that equals to its reverse.


Examples of palindrome numbers and non-palindrome numbers:

Example #1
121 is palindrome number:

121
reverse of the number 121 is also 121 which means the number is palindrome.

Example #2
12321 is palindrome number:

12321
reverse of the number 12321 is also 12321 which means the number is palindrome.

Example #3
123 is not a palindrome number:

123
reverse of the number 123 is 321 which means the number is not a palindrome.

Here i will explain the code with an example.

number = input("Enter a number:")

The input() function is used to get input from the user during runtime. 
For example: if the user input is 121, it will be assigned to the variable 'number' as str(string) object.
number  = "121"

number = int(number)
The number "121" which is stored as string is now get converted into integer using int() function and is assigned to the variable 'number'.
number = 121

temp = number
The intrger value 121 stored in the varible 'number' is also assigned to a temporary variable 'temp'.
temp = 121

sum = 0
The variable 'sum' is initialised to zero.

while number > 0:
    rem = number % 10
    sum = sum*10 + rem
    number = number//10
    
#First loop
	While number > 0:
    => 	while 121 > 0: which is True, therefore, the body of the loop will be executed:
	rem = 121 % 10
    =>	rem = 1
    
By doing the above operation, the rem variable is assigned with the last digit of the number 121, ie. number 3

	sum = sum*10 + rem
    => 	sum = 0*10 + 1
    =>	sum = 0 + 1
    =>	sum = 1

By doing the above operation, the variable is stored with the value of 'sum multiplied by 10 + remainder'. Currently the value of the variable 'sum' is 0. So, 0 multiplied by 10 + 1 is assigned to the variable sum, ie. 1.

	number  = number//10
	number  = 121 // 10
	number  = 12
	
Before going to the next loop, we need to have only the first two digits of number 121 because the operation on last digit is already performed in the current loop(first loop). The floor division of number 121 by 10 is 12 (integer value is only assigned back to variable 'number'). 

By the end of the first loop, the current values of variables are as shown below.

sum = 1
number = 12

#Second loop
	While number > 0:
    => 	while 12 > 0: which is True, therefore, the body of the loop will be executed:
	rem = 12 % 10
    =>	rem = 2
    
By doing the above operation, the rem variable is assigned with the second digit of the three digit number 121, ie. number 2

	sum = sum*10 + rem
    => 	sum = 1*10 + 2
    =>	sum = 10 + 2
    =>	sum = 12

By doing the above operation, the variable is stored with the value of 'sum multiplied by 10 + remainder'. Currently the value of the variable 'sum' is 1. So, 1 multiplied by 10 + 2 is assigned to the variable sum, ie. 12.

	number  = number//10
	number  = 12 // 10
	number  = 1
	
Before going to the next loop, we need to have only the first digit of number 12 because the operation on digit 2 is already performed in the current loop(second loop). The floor division of number 12 by 10 is 1 (integer value is only assigned back to variable 'number'). 

By the end of the second loop, the current values of variables are as shown below.

sum = 12
number = 1

#Third loop
	While number > 0:
    => 	while 1 > 0: which is True, therefore, the body of the loop will be executed:
	rem = 1 % 10
    =>	rem = 1
    
By doing the above operation, the rem variable is assigned with the first digit of the three digit number 121, ie. number 1

	sum = sum*10 + rem
    => 	sum = 12*10 + 1
    =>	sum = 120 + 1
    =>	sum = 121

By doing the above operation, the variable is stored with the value of 'sum multiplied by 10 + remainder'. Currently the value of the variable 'sum' is 12. So, 12 multiplied by 10 + 1 is assigned to the variable sum, ie. 121.

	number  = number//10
	number  = 1 // 10
	number  = 0
	
Before going to the next loop, we need to have only the remaining digits of number 1 because the operation on digit 1 is already performed in the current loop(third loop). The floor division of number 1 by 10 is 0 (integer value is only assigned back to variable 'number'). But the value of variable 'number' is now zero, which means operations are performed on all the digits of the number 121 and no more operations are left unperformed. 

By the end of the third loop, the current values of variables are as shown below.

sum = 121
number = 0

#Fourth loop [not executed]
	While number > 0:
    => 	while 0 > 0: which is False, therefore, the body of the loop will not be executed and control of the loop comes outside the loop.
    

Now we need to compare the value of the variable 'sum' with the actual number inputted by the user, ie.121. But the variable 'number' is not having the value 121 intially assigned by the user. But we have already have a copy the number 121 that is assigned to variable 'temp'.

if temp == sum:
    print("Palindrome")
else:
    print("Not Palindrome")
		 

So we need to compare the values of variables 'sum' and 'temp'.
If both are same, then it's a palindrome number. Otherwise it's not.

	if temp == sum:
    => 	if 121 == 121: which is True, therefore, the body the if conditional statement will be executed. ie. 'print("Palindrome")' will get executed and the output will be 'palindrome'.