Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll see how to reverse a number using a mathematical approach in Java. First, we’ll see what math operations are necessary for doing this, and then we’ll go through three different ways of implementing this.

2. Solution Approach

To start off, let’s take an example and see exactly what should happen. For example, we want the number 1234 to become 4321. This can be achieved with the following approach:

  1. get the last digit of the number
    • we can apply modulus to get the last digit
    • 1st iteration – 1234 % 10 = 4
    • 2nd iteration – 123 % 10 = 3
  2. multiply the reversed number by 10 and add the digit found in the previous step
    • 1st iteration – 0 * 10 + 4 = 4 (since there’s no reversed number to start with, we multiply with 0 in the 1st iteration)
    • 2nd iteration – 4 * 10 + 3 = 43
  3. divide the original number by 10 and repeat from step 1 and continue until the number is not 0
    • 1st iteration – 1234 / 10 = 123
    • 2nd iteration – 123 / 10 = 12

3. Mathematical Implementation

We want to translate the math operations above into code. This is possible in three different ways: using a while loop, a for loop, or recursion.

The approaches below also cater to negative values by using the absolute value of the number to be reversed and multiplying the reversed number by -1 if the original number is negative.

3.1. while Loop

The while loop is first on the list as it’s the clearest way of translating the math operations above:

int reversedNumber = 0;
int numberToReverse = Math.abs(number);

while (numberToReverse > 0) {
    int mod = numberToReverse % 10;
    reversedNumber = reversedNumber * 10 + mod;
    numberToReverse /= 10;
}

return number < 0 ? reversedNumber * -1 : reversedNumber;

3.2. for Loop

Using the for loop, the logic is the same as earlier. We skip the initialization statement of the for loop and use the number that is being reversed in the terminating condition:

int reversedNumber = 0;
int numberToReverse = Math.abs(number);

for (; numberToReverse > 0; numberToReverse /= 10) {
    int mod = numberToReverse % 10;
    reversedNumber = reversedNumber * 10 + mod;
}

return number < 0 ? reversedNumber * -1 : reversedNumber;

3.3. Recursion

For recursion, we can use a wrapper method that calls the recursive method that returns the reversed number:

int reverseNumberRecWrapper(int number) {
    int output = reverseNumberRec(Math.abs(number), 0);
    return number < 0 ? output * -1 : output;
}

The recursion method implements the math operations in the same manner as the previous examples:

int reverseNumberRec(int numberToReverse, int recursiveReversedNumber) {

    if (numberToReverse > 0) {
        int mod = numberToReverse % 10;
        recursiveReversedNumber = recursiveReversedNumber * 10 + mod;
        numberToReverse /= 10;
        return reverseNumberRec(numberToReverse, recursiveReversedNumber);
    }

    return recursiveReversedNumber;
}

The recursive method returns the currently reversed number in each iteration and the number to be reversed is shortened with each iteration. This happens until the number to be reversed is 0, at which time we return the fully reversed number.

4. Conclusion

In this article, we explored three different implementations for reversing a number, using a while loop, a for loop, and recursion.

As always, the source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.