Q:

Python program to check Palindrome number using object oriented approach

belongs to collection: Python class & object programs

0

Palindrome Number: The number which is equal to reverse number know as Palindrome Number. For example Number 12321 is a Palindrome Number, because 12321 is equal to its reverse Number 12321.

Steps for checking Palindrome number:

  1. Find reverse of the given number.
  2. Compare that number with the reverse number.
  3. If number and its reverse is equal then it is a Palindrome Number otherwise not.

We are implementing this program using the concept of classes and objects.

Firstly, we create the Class with "Check" name with 1 attributes (number) and 2 methods, the methods are:

  1. Constructor Method: This is created using __init__ inbuilt keyword. The constructor method is used to initialize the attributes of the class at the time of object creation.
  2. Object MethodisPalindrome() is the object method, for creating object method we have to pass at least one parameter i.e. self keyword at the time of function creation. This Object method has no use in this program.

Secondly, we have to create an object of this class using a class name with parenthesis then we have to call its method for our output.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Student height record program for a school in Pyth... >>
<< Python program to check Armstrong number using obj...