# Reverse a number in java

In this tutorial, we will learn about how to reverse a number in java.

We can write a program to reverse a number in java in a lot of ways. . Letβs take a detailed look at all the approaches to reverse number in Java.

• Reverse a Number in Java using While Loop
• Reverse a Number in Java using For Loop
• Reverse a Number in Java using Recursion

## Java program to reverse a number using while loop

To reverse a number, we are using while loop and some arithmetic operations. The idea is to extract each digit of the number one by one, starting from the last digit, and concatenate them in reverse order.

import java.util.Scanner;
import java.util.NoSuchElementException;

public class MyClass
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
try {
int m, n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number : ");
m = s.nextInt();
while(m > 0)
{
n = m % 10;
sum = sum * 10 + n;
m = m / 10;
}
System.out.println("Reverse of a Number is : "+sum);
}
catch (NoSuchElementException e)
{
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}

### Output :

Enter the number : 2598
Reverse of a number : 9852

In the above program, first the remainder of the number is divided by 10 and then stored in the βiβ variable. Now, the βiβ variable contains last digit of the number that we have entered which is 2.

Then, we add i to reverse and multiply it by 10. As a result, it adds new place in the reversed number. Hence, the onesβ place is multiplied by 10 gives us the tenth place of the number. Similarly, the tenth places will give the hundreds place and so on.  In this way, the iterations happen and the digit is reversed.

### Time complexity :O(n)

The time complexity of this program is O(n), where n is the number of digits in the number.

### Space Complexity :O(1)

Because no extra space is used to store the digit, the space complexity of this program is O(1)

## Reverse the number using for loop

In this Program we will go through that how to reverse a number in java using for loop.

import java.util.Scanner;
import java.util.NoSuchElementException;

public class MyClass
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
try {
int m, n, reversed = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number: ");
m = s.nextInt();
for (; m != 0; m /= 10)
{
n = m % 10;
reversed = reversed * 10 + n;
}
System.out.println("Reverse of a Number: " + reversed);
}
catch (NoSuchElementException e)
{
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}

### Output :

Enter the number: 2598
Reverse of a Number: 9852

Check out My Latest post on Developer Helps for some Interesting Insights

In the program using or loop, we do not use any initialization expression. Here, the test expression remains the same as while loop which is num!=0. we increment the expression as num/=10. As a result, after each iteration the new expression keeps on removing the last digit of the number. So, the final output will contain the reversed number, as we finish the loop.

### Time Complexity :O(log n)

The time complexity of the Java program that reverses a number using a for loop is O(log n), as it involves iterating through the digits of the number.

### Space Complexity :O(1)

The space complexity is O(1), as it uses a fixed amount of memory to store the variables.

## We can also perform the reverse of a number using recursion.

import java.util.Scanner;

public class Developer_helps {
public static void main(String[] args) {
int n, count = 0, m;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:\n");
System.out.print("Type something in the Stdin box aboveβ¦. and then Execute\n");
n = s.nextInt();
m = n;
while (m > 0) {
count++;
m = m / 10;
}
Developer_helps obj = new Developer_helps();
int a = obj.reverse(n, count);
System.out.println("Reverse of a Number: " + a);
}

int reverse(int x, int length) {
if (length == 1) {
return x;
} else {
int b = x % 10;
x = x / 10;
return (int) ((b * Math.pow(10, length - 1)) + reverse(x, --length));
}
}
}

### Output :

Enter the number : 2598
Reverse of a Number : 9852

### Time Complexity :O(log n)

The time complexity of this program is O(log n), where n is the input number. This is because the number of recursive calls is proportional to the number of digits in the input number, which is logarithmic in base 10.

### Space Complexity :O(log n)

The space complexity of this program is also O(log n), because the recursive calls consume memory on the call stack, and the maximum depth of the call stack is proportional to the number of digits in the input number, which is logarithmic in base 10.

Check out My Latest post on Developer Helps for some Interesting Insights

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.