Reversing a Number


We will see three different ways of reversing a number.
Let the input number be n. We initialize the result to zero. When the program finishes execution, 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 digit would 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 in nature, we use a while number with the loop condition being n > 0, where n is the input number. The last digit of n can is the same as the remainder obtained on 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 an int and not a decimal. The final step is to add the extracted digit to the result. This can be done by multiplying the result with 10 and adding the extracted digit 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 which 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 which takes an 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 in the beginning of the program would be initialised to an empty String. Finally, the reversed String can be converted back to an int using the parseInt(String) method of 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. This reversing operation can be done in more convenient way by using the StringBuffer class and its method reverse(). We first construct a String using the input integer. A StringBuffer object is then constructed using the String. These two steps can be combined 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 an integer is passed as an input, that integer would be taken as the initial length of the StringBuffer.
The integer n concatenated with an empty String results in a String which is passed to the constructor of StringBuffer. The reverse() process is then invoked on the StringBuffer object which will reverse its contents. Now, the StringBuffer is converted back to a String and then to an 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 synchronised 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;
}
Author: , 0000-00-00