Python Program to reverse a number using class and object


In this program, you will learn how to reverse a number using class and object in Python.


123 => 321

456 => 654

Example: How to reverse a number using class and object in Python

class Test:

    def reverse(self, num):
        rev = 0
        while num > 0:
            r = num % 10
            rev = rev * 10 + r
            num = int(num / 10)
        return rev


num = int(input("Enter a number:"))
obj = Test()
rev = obj.reverse(num)
print("Reverse number is:", rev)

Output:

Enter a number:123
Reverse number is: 321