public class ReverseDigits {
/*
Problem: Given a number reverse it. Ex. input 321 output should be 123
Solution:- Take % of x with 10 at a time and add it to result.
*/
public long revereseDigits(int x){
long result = 0;
int xRemaining = Math.abs(x);
while (xRemaining != 0){
result = result * 10 + xRemaining%10;
xRemaining = xRemaining/10;
}
return result;
}
}
No comments:
Post a Comment