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.

## Understand with Example

So, reversing a number means swapping the digits. For instance, if the input is 98634, the output will be 43689 as shown below.

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

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

- Initialize the variable reverse = 0.
- Take the modulus of the number by 10. This will be stored in the variable rem i.e. remainder.
- Now, do rev = rev * 10 + rem.
- Divide the number by 10.
- 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 the code for the same.

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:** 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 (Auxiliary Space):** Since we have not used any extra data structure 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 will apply the recursive call as shown below.

So, let us now write the code for the same.

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:** The time complexity will be O(log10N) because the number of recursion calls depends upon the number of digits in the number.

**Space complexity (Recursion Space and Auxiliary Space):** 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).

So, these are the 2 methods to reverse a number in Java. We hope that you have understood this solution and the concepts. Hope to see you again at PrepBytes.