/**
* @param args
*/ public static void main(String[] args) { long [] nums = { 12345678, 786, 12, 1, -2, -34, -386 }; for (long num : nums) {
System.out.println("Reverse of number '" + num + "' : " + reverseNumber(num));
}
}
private static long reverseNumber(long number) { long revNum = 0;
while (number % 10 != 0) {
revNum = (revNum * 10) + (number % 10);
number = number/10;
}
return revNum;
}
}
It gives the following output,
Reverse of number '12345678' : 87654321
Reverse of number '786' : 687
Reverse of number '12' : 21
Reverse of number '1' : 1
Reverse of number '-2' : -2
Reverse of number '-34' : -43
Reverse of number '-386' : -683