Interview Question

Interview New York, NY

How do you reverse a number without storing it in an array

  Ex: 412 should be printed as 214

Interview Answer

3 Answers


I gave an answer but they told me that I am using un-necessary multiplications and divisions,

Interview Candidate on Feb 15, 2012

int reverse(int n){ int rev = 0; while(n!=0){ rev = rev*10 + n%10; n = n/10; } return rev; }

Anonymous on Oct 3, 2012

int reverse(int n){ if(n==0) return; int tmp=n%10; reverse(n/10); printf("%d",tmp);}

yifeng on Dec 6, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.