Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Reverse Integer

public class HelloWorld{

    public static void main(String []args){
        System.out.println("Max " + Integer.MAX_VALUE);
        System.out.println("Min " + Integer.MIN_VALUE);
        System.out.println("Reverse " + reverse1(1000000007));
        System.out.println("Reverse " + reverse1(-1234567890));
    }
    
    static int reverse2(int x) {
        long rev= 0;
        while( x != 0){
            rev= rev*10 + x % 10;
            x= x/10;
            if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
                return 0;
        }
        return (int) rev;
    }
    static int reverse1(int num) {
        long result = 0;
        
        while(num != 0) {
            // int temp = num % 10;
            result = (result * 10) + (num % 10);
            // System.out.println(result);
            // System.out.println((newResult - temp) /10);
            // if((newResult - temp) /10 != result) {
            if( result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
                return 0;
            }

            // result = newResult;
            num = num / 10;
        }
        return (int)result;
    }
    
    

    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.