Table of Contents

Python code to reverse an integer number.

 

Python code implementation without user defined functions & classes

Code: 
#Take an integer number
a = 34502

#Convert the number in to String
b = str(a)

#Reverse the string and store it in b
b = b[::-1]

#Convert string to Integer
a = int(b)

#Print the reverse integer
print("The reverse integer number is: ",a)

 

Output: 
The reverse integer number is:  20543
Python Code Editor Online - Click to Expand

 

Python code implementation using function

In this code we will input an integer number using console. We will pass the number to a function that will return the reverse number.

Code: 
#Python code to reverse an interger number using Function

def reverse_number(number):
    # Convert the number in to String
    number_string = str(number)
    # Reverse the string and store it in number_string
    number_string = number_string[::-1]
    return int(number_string)

# Take an integer number
a = 128250468


#Call the Function by passing the given number

print("The reverse integer number is: ", reverse_number(a))

 

Output: 
The reverse integer number is:  864052821
Python Code Editor Online - Click to Expand

 

Python code implementation using Classes

In this code we will input an integer number using console. We will implement a class that contain function to return the revere of number. We will create an object and will call the function using object.function(), the will return the reverse integer number.

Code: 
#Python code to reverse an interger number using classes

class Reverse_number_class(object):
    def __init__(self):
        pass

    def reverse_number(self, number):
        self.number = number
        # Convert the number in to String
        number_string = str(self.number)
        # Reverse the string and store it in number_string
        number_string = number_string[::-1]
        return int(number_string)



# Take an integer number
a = 128250468

#Create an Object
object_reverse = Reverse_number_class()

#Call the Function

print("The reverse integer number is: ", object_reverse.reverse_number(a))

 

Output: 
The reverse integer number is:  864052821
Python Code Editor Online - Click to Expand

 

Enjoy Python Code By Pythonbaba 🙂