Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Interview Question

Interview Seattle, WA

Write code to reverse the digits in an integer.


Interview Answer

3 Answers


int reverse(int a) { int b = 0; while (a>0) { b *= 10; b += a%10; a /= 10; } return b; }

no one on Mar 1, 2013

I am not sure this solution would work because if a was 93, your program would produce the answer 39.3, but I am not really a software guy so idk. my logic is that a=93 93%10 = 3 b=3 a=9.3 *next iteration* b=30 9.3%10 = 9.3 b = 39.3 is this correct? just as a disclaimer, I am not trying be an jerk, I am just practicing interview questions lol

Robert Hannum on Jun 12, 2015

To Robert: Remember integer division will truncate the value, so 93/10 = 9, not 9.3.

Anonymous on Jul 1, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.