  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Java Program to Reverse a Number

Last Updated on May 16, 2023 by Prepbytes Reversing a number means swapping the digits. For instance, if the input is 98634, the output will be 43689, as shown below. In this article, we will learn to reverse a number in Java. We will understand how to reverse a number in Java, and we will write the program to reverse a number in Java.

## Approach 1: Reverse a Number in Java using While-Loop

In order to reverse a number in Java, we will follow the following algorithm.

1. Initialize the variable reverse = 0.
2. Take the modulus of the number by 10. This will be stored in the variable rem i.e. remainder.
3. Now, do rev = rev * 10 + rem.
4. Divide the number by 10.
5. Repeat the steps from 2 to 4 till the number becomes 0.

So, let us take example 98634 as shown below. So, in the first step, we have rev = 4, and the number is reduced to 9863. Now, let us divide the number by 10 again. So, in the second step, we have rev = 43, and the number is reduced to 986. Now, let us divide the number by 10 again. So, in the third step, we have rev = 436, and the number is reduced to 98. Now, let us divide the number by 10 again. So, in the fourth step, we have rev = 4368, and the number is reduced to 9. Now, let us divide the number by 10 again. So, in the fourth step, we have rev = 43689, and the number is reduced to 0. Since the number has now become 0, we will stop the division.

Now that we have understood the procedure, let us write a program to reverse a number in java.

```import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int number = scn.nextInt();
int rev = 0;

while(number != 0) {
int rem = number % 10;
rev = rev * 10 + rem;
number /= 10;
}

System.out.println(rev);
}
}
```

Time Complexity of Reverse a Number in Java: The time complexity of this number is O(log10N). This is because the while loop iterates the same number of times as the digits in the input number and i.e. log10N.

Space Complexity of Reverse a Number in Java : Since we have not used any extra data structures or extra space, the space complexity is O(1).

So, this is how we can reverse a number in Java using a while loop. Now, we know that something that can be done using a loop can also be done using recursion. So, let us now look at the recursive solution to reverse a number in Java.

## Approach 2 – Reverse a Number in Java using Recursion

In recursion, we will keep a static variable rev outside the recursive function and apply the recursive call as shown below. Now that we have understood the procedure, let us write a program to reverse a number in java.

```import java.util.*;

public class Main {

static int rev;

public static void reverse(int number) {
if(number == 0) return;

int rem = number % 10;
rev = rev * 10 + rem;
reverse(number/10);
}

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int number = scn.nextInt();
rev = 0;
reverse(number);
System.out.println(rev);
}
}
```

Time Complexity of reversing a number in Java:
The time complexity will be O(log10N) because the number of recursion calls depends on the number of digits in the number.

Space Complexity of reversing a number in Java:
The auxiliary space is O(1), as we have not used any extra space. However, the recursion space is O(log10N) because the height of the recursion tree will be O(log10N).

Conclusion
To summarize, this article discussed various methods for reversing a number in Java. The first method used a while-loop to extract each digit of the number using modulus and then add it to the reverse variable by multiplying it by 10. The number was divided by ten until it reached zero. The second approach investigated a recursive solution, which made use of a static variable to keep track of the reversed number. Both approaches have a time complexity of O(log10N), where N is the input number, and a space complexity of O(1) for the iterative approach and O(log10N) for the recursive approach. Understanding these techniques allows one to effectively reverse a number in Java, depending on the needs of their program.

Q1. What is the math function to reverse a number in Java?
Ans. The reverse() method of the Java Integer class numerically returns the value obtained by reversing the order of the bits in the 2’s complement binary representation of the specified integer value.

Q2. How do you reverse a math formula?
Ans.