Reverse a Number in Java.

Reverse a Number in Java.


Given a integer number, reverse it.

Lets see sample input and output for better understanding:
Reverse a number in Java input output example
Reverse a number in Java input output example

Algorithm 


Reversing a number is very easy, start collecting the digits from the back of the number until no digits is remaining.

How we will get the last digit of a number?
when a number is Mod by 10, we will get the last digit of a number. 
Example  int lastDigit = number % 10.

We just collected the last digit, so now we need to get the remaining number to work on except the last digit which we already worked on, how we will get the remaining number except last digit?
By dividing the number by 10. Example: int remainingNumber = number / 10.

We need to collect all the last digits we get in reverse order, how to do that?
int reversedNumber = reversedNumber * 10 + lastDigit;

Continue above steps in loop  until remaining number is 0.

Note: We may hit integer overflow when we do (reversedNumber * 10 + lastDigit), so make sure before executing that line we check for overflow.

1. reversedNumber * 10 can overflow, so check reversedNumber  > Integer.MAX_VALUE / 10, if yes then (reversedNumber * 10) will surely overflow.

2. reversedNumber * 10 + lastDigit
Adding the + lastDigit can also overflow, if reversedNumber is exactly equal to Integer.MAX_VALUE / 10 then we need to check the + lastDigit we add should be less that 7 (for positive number) because in Java, the integer ranges from -2,147,483,648 to +2,147,483,647 and greater than -8 in case negative number.

Note: Program below consider 0 int the front of the number as integer as is not necessary to display.
Eg: if the integer to reverse is 100 then output would not be 001 instead it will be 1.

Java Program to reverse Integer in Java

package com.javabypatel;

/*
    Input:  214748364
    Output: 463847412

    Input: 0
    Output: 0

    Input: -123
    Output: -321

 */
public class ReverseInteger {
    public static void main(String[] args) {

        int num = -123;
        System.out.println(reverseInteger(num));

        System.out.println(Integer.MAX_VALUE / 10);
        System.out.println(214748364 * 10);
    }

    private static int reverseInteger(int number) {
        int result = 0;
        while (number != 0) {
            int remainder = number % 10;
            number = number / 10;

            //In Java, the integer ranges from -2,147,483,648 to +2,147,483,647
            if (result > (Integer.MAX_VALUE / 10) || (result == Integer.MAX_VALUE / 10 && remainder > 7)) {
                return 0;
            }

            if (result < (Integer.MIN_VALUE / 10) || (result == Integer.MIN_VALUE / 10 && remainder < -8)) {
                return 0;
            }

            result = result * 10 + remainder;
        }
        return result;
    }
}

You may also like to see


Reverse String in Java

Bubble Sort

Heap Sort

Selection Sort

Insertion Sort


How ConcurrentHashMap works and ConcurrentHashMap interview questions

How Much Water Can A Bar Graph with different heights can Hold

Interview Questions-Answer Bank

Enjoy !!!! 
If you find any issue in post or face any error while implementing, Please comment.

Post a Comment