How do you reverse a number without storing it in an array
Ex: 412 should be printed as 214
Interview Answers
Anonymous
Oct 4, 2012
int reverse(int n){
int rev = 0;
while(n!=0){
rev = rev*10 + n%10;
n = n/10;
}
return rev;
}
2
Anonymous
Dec 7, 2012
int reverse(int n){
if(n==0) return;
int tmp=n%10;
reverse(n/10);
printf("%d",tmp);}
Anonymous
Nov 16, 2017
Arithmetic operations are unnecessary. If it is required to use an array, have variable i from start and variable j from the end walk towards center of the array and swap content of a[i] and a[j].
Anonymous
Feb 15, 2012
I gave an answer but they told me that I am using un-necessary multiplications and divisions,