Learn about Program to Reverse a Negative Number in Python in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Program to Reverse a Negative Number in Python
# Python program to reverse a negative number
def reverseNum(n):
n = str(n)
if n[0] == '-':
a = n[::-1]
return f"{n[0]}{a[:-1]}"
else:
return n[::-1]
# input
num = '-123'
# function call
print('The reverse number is =', reverseNum(num))
Output:
The reverse number is = -321
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Prime Factorization Python Program using Function