Sunday, October 30, 2011

function to reverse digits of an integer

//Write a function to reverse digits of an integer. E.g. 456 --> 654, -7800 --> -87.
int ReverseDigits (int n){
    int ret = 0;
    while (n){
        ret = ret * 10 + n % 10;  //mod 10.
        n /= 10;  //div
    }
    return ret;
}

No comments: