Checking palindrome number: Here, we will figure out how to check whether a given number is a Palindrome number or not utilizing class and objects (object-arranged methodology)?
This program will take a number and check whether it is a palindrome number or not?
Palindrome Number: The number which is equivalent to invert number know as Palindrome Number. For instance, Number 12321 is a Palindrome Number, in light of the fact that 12321 is equivalent to its invert Number 12321.
Steps for checking Palindrome number:
Discover turn around of the given number.
Contrast that number and the turn around the number.
On the off chance that number and its switch is equivalent, at that point it is a Palindrome Number generally not.
We are executing this program utilizing the idea of classes and objects.
Initially, we make the Class with “Check” name with 1 characteristic (number) and 2 methods, the methods are:
Constructor Method: This is made utilizing init inbuilt watchword. The constructor method is utilized to introduce the traits of the class at the hour of object creation.
Object Method: isPalindrome() is the object method, for making object method we need to go at any rate one parameter for example self catchphrase at the hour of capacity creation. This Object method has no utilization in this program.
Besides, we need to make an object of this class utilizing a class name with bracket then we need to call its method for our output.
The following is the usage of the program,
Python code to check palindrome number:
# Define a class for Checking Palindrome number
class Check :
# Constructor
def __init__(self,number) :
self.num = number
# define a method for checking number is Palindrome or not
def isPalindrome(self) :
# copy num attribute to the temp local variable
temp = self.num
# initialise local variable result to zero
result = 0
# run the loop untill temp is not equal to zero
while(temp != 0) :
rem = temp % 10
result = result * 10 + rem
# integer division
temp //= 10
# check result equal to the num attribute or not
if self.num == result :
print(self.num,"is Palindrome")
else :
print(self.num,"is not Palindrome")
# Main code
if __name__ == "__main__" :
# input number
num = 151
# make an object of Check class
check_Palindrome = Check(num)
# check_Palindrome object's method call
check_Palindrome.isPalindrome()
num = 127
check_Palindrome = Check(num)
check_Palindrome.isPalindrome()
Output:
151 is Palindrome
127 is not Palindrome