reverse integer in java


  • 0
    E

    when i enter for instance int x = 123;
    and x%10 the rest number became 3 and store it in reminder
    and store it int reverse "reverse = reverse*10 +reminder"
    and divide x/10 and the result will be 12
    then repeat that until x = 0

    public static int reverse(int x)
        {
            int reverse = 0;
            int reminder;
            while(x !=0)
            {
                reminder = x %10;
                reverse = reverse *10 + reminder;
                x = x/10;
            }
            return reverse;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.