How to reverse digits of integer?

  • A+
Category:Languages
public static int reverse(int n) {     int result = 0;     while (n > 0) {         result = result * 10 + n % 10;         n = n / 10;     }     return result; } 

I'm trying to reverse the digits of integer. Instead of doing the codes like what I have done, is there any other way to do it? Can i reverse it using java stream?

 


Another way would be

int digits = 12345; StringBuilder buf = new StringBuilder(String.valueOf(digits)); System.out.println(buf.reverse()); System.out.println(Integer.valueOf(buf.toString())); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: