Thursday, July 07, 2016

reverse an integer

This works for -ve ints too (as -9 % 10 = -9). checked to throw overflow ex for int 1000000009.
(x):
    checked:
        y = 0
        while x != 0:
            y = y *10 + (x %10)
            x = x /10
        return y
[Hat tip to S]

No comments:

Post a Comment