Java Program to Reverse a Number

Updated on September 30, 2024
Reverse a Number header image

Introduction

Reversing a number is a common task given in programming interviews, entrance tests, and academic projects primarily because it allows demonstrating understanding of fundamental programming concepts such as loops and mathematical operations. In Java, several methods can be utilized to reverse a number, including iterative methods and converting the number into a string and reversing that string.

In this article, you will learn how to efficiently reverse a number in Java using different methods. Explore practical examples to grasp how each method works and when to use them effectively.

Reversing a Number Using Loops

Reverse an Integer Using a While Loop

  1. Initialize a variable to store the reverse of the number.

  2. Use a while loop to extract each digit of the number.

  3. In each iteration, multiply the reverse variable by 10 and add the current last digit.

    java
    public int reverseNumber(int number) {
        int reverse = 0;
        while (number != 0) {
            int remainder = number % 10;
            reverse = reverse * 10 + remainder;
            number = number / 10;
        }
        return reverse;
    }
    

    In this code, as long as the number is not zero, the last digit is peeled off using modulus operation, added to the reverse, and the original number is reduced by dividing by 10. This loop effectively places the last digit of the number at the front, achieving the reverse.

Reversing a Number Using a For Loop

  1. Convert the integer to a string to utilize the string length in the loop.

  2. Iterate over the string from the last character to the first.

  3. Concatenate each character to a new string to form the reversed number.

    java
    public int reverseNumberUsingForLoop(int number) {
        String numberStr = Integer.toString(number);
        String reversedStr = "";
        for (int i = numberStr.length() - 1; i >= 0; i--) {
            reversedStr += numberStr.charAt(i);
        }
        return Integer.parseInt(reversedStr);
    }
    

    This method treats the number as a string, reverses it through a simple for loop, and then converts the reversed string back into an integer. This method is straightforward but might not be as efficient as the first due to string immutability and concatenation overhead in Java.

Advanced Techniques to Reverse a Number

Using StringBuffer to Reverse a Number

  1. Convert the number to a string.

  2. Create a StringBuffer object from the string.

  3. Utilize the reverse() method of StringBuffer.

    java
    public int reverseNumberWithStringBuffer(int number) {
        StringBuffer sb = new StringBuffer(String.valueOf(number));
        sb.reverse();
        return Integer.parseInt(sb.toString());
    }
    

    This approach makes use of Java's StringBuffer, which is mutable and designed for such operations. The reverse() method directly reverses the contents of the StringBuffer, which is a neat and effective way to perform this operation.

Conclusion

Reversing a number in Java can be approached in several ways, each having its own set of advantages depending on the situation. Utilizing loops provides a more educational insight into how numbers can be manipulated using basic operations while methods using StringBuffer or StringBuilder cater to more concise and potentially more readable solutions. Experiment with these techniques to enhance your understanding of Java and its robust set of functionalities for handling numeric data.