Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!