Wednesday, April 9, 2014

reverse integer

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Solution:
positve or negative doesn't matter here. Cause % or / will have the same sign as original number.
Code:
    int reverse(int x) {
   int result=0;
   while(x!=0)
   {
       result =result*10+ x%10;
       x = x/10;
   }
   return result;
    }

No comments:

Post a Comment