Python program to check Palindrome number using object oriented approach

Checking palindrome number: Here, we are going to learn how to check whether a given number is a Palindrome number or not using class & objects (object-oriented approach)?
Submitted by Ankit Rai, on July 23, 2019

This program will take a number and check whether it is palindrome number or not?

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 Method: isPalindrome() 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.

Below is the implementation 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
    num = 127
    check_Palindrome = Check(num)


151 is Palindrome
127 is not Palindrome

Python class & object programs »


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.