Reversing a Number in JavaWe will see three different ways of reversing a number.

Reversing a Number using Mathematical Operations

Let the input number be n. We initialized the result to zero. When the program finishes execution, the result will contain the reversed number. We extract the digits of the number starting from the right one by one and add it to the result. The extracted digits will be removed from the original number. This task needs to be performed repeatedly until no more digits are left in the original number.

Since this task is repetitive, we use a while number with the loop condition being n > 0, where n is the input number. The last digit of n can be the same as the rest received by dividing the number by 10. We use the modulo operator for this purpose. To remove the extracted number from n, we divide n by 10. Note that since both n and the number we are dividing with (10) are int values, the resulting value will be int and not decimal.

The last step is to add the extracted digit to the result. This can be done by multiplying the results by 10 and adding the extracted digits to it. To illustrate the above steps, let us take an example where a particular iteration n is 97 and the result till that point of time is 34. 97% 10 gives 7 ( rem ). Diving n (97) by 10 makes it 9. And finally, adding 7 to the result gives 34*10+7=347.

Given below is a method that takes an integer parameter n, reverses it, and returns the result.

public void reverse(int n) {
   int result = 0;
   int rem;
   while (n > 0) {
       rem = n % 10;
       n = n / 10;
       result = result * 10 + rem;
   }
}

Here is a complete program that takes integer input from the user and displays the reversed number on the screen.

public class ReverseNumber {

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.print(“Enter the number to be reversed : “);
       int input = s.nextInt();
       int result = reverse(input);
       System.out.println(“The reversed number is ” + result);
   }

   public static int reverse(int n) {
       int result = 0;
       int rem;
       while (n > 0) {
           rem = n % 10;
           n = n / 10;
           result = result * 10 + rem;
       }
       return result;
   }
}

Here is a sample execution.

Enter the number to be reversed: 347
The reversed number is 743

Reversing a Number using String operations

To reverse a number using String operations, we first convert the int value to a String using the static method, valueOf(int) of the String class. Next, we extract the characters of the String from the right, one by one, and append it to the result, which at the beginning of the program would be initialized to an empty String. Finally, the reverse String can be converted back to int using the parseInt(String) method of the Integer class.

Here is a method implementing the above procedure.

public static int reverse(int n) {
   String input = String.valueOf(n);
   String result = “”;
   for (int i = input.length() – 1; i >= 0; i–) {
       result = result + input.charAt(i);
   }
   int reversedInt = Integer.parseInt(result);
   return reversedInt;
}

Reversing a Number using StringBuilder

In the previous method, we have reversed the String manually by reading the characters from right to left and adding them in that order to the result String. We can do this reversing operation in a more convenient way by using the StringBuffer class and its method reverse(). We first construct a String using the input integer. We then constructed a StringBuffer object using the String. We can combine these two steps into a single step in the following way:

StringBuffer s = new StringBuffer(n+””);

However, the following statement will give incorrect results.

StringBuffer s = new StringBuffer(n);

This is because the constructor of StringBuffer requires a String as its input from which the StringBuffer is to be constructed. If it passes an integer as an input, then it would take an integer as the initial length of the StringBuffer.

The integer n concatenated with an empty String results in a String that is passed to the constructor of StringBuffer. The reverse() process is then invoked on the StringBuffer object which will reverse its contents. StringBuffer is converted back to String and then to int using toString() and parseInt() methods respectively.

We can also use the StringBuilder class instead of the StringBuffer class. The difference between these two classes is that StringBuffer is synchronized while StringBuilder is not.

Here is the complete method illustrating the above procedure.

public static int reverse(int n) {
   String inputString = String.valueOf(n);
   StringBuffer stringBuffer = new StringBuffer(inputString);
   stringBuffer.reverse();
   String reversedString = stringBuffer.toString();
   int reversedInt = Integer.parseInt(reversedString);
   return reversedInt;
}