Jump to content

[PYTHON] Reverse Integer


Tupi
 Share

Recommended Posts

Function:

def reverseInt(x):
    string = str(x)
    
    if string[0] == '-':
        return int('-'+string[:0:-1])
    else:
        return int(string[::-1])

 

Input:

integer = 2008
print("Integer:", integer)
print("Reverse Integer:", reverseInt(integer))

 

Output:

Integer: 2008
Reverse Integer: 8002

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.