# Reverse Number In Java

What We Are Learn:

In this tutorial, you’ll learn how to reverse a number in Java. You’ll see how to do it with different techniques and find out which one is the most efficient for your needs.

## Reverse Number In Java

We’ve talked about the different ways you can reverse a number in a Java program. The compiler is added to each and every program so it can be executed. Examples are given with accompanying sample outputs.

Using While Loop
Using Static Method
Using Recursion
Using For Loop
Using Function
Without Using the Modulus Operator

### Reverse Number In Java Using While Loop

This method will allow us to use a while loop to break down the number input and rearrange it in reverse order. We’ll be using the modulo operator to extract digits from the number, and the divide operator to shorten it.

```package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx1
{
public static void main(String[] args)
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}

System.out.println("Reverse of input number is: "+reversenum);
}
}```

Output:

```Input your number and press enter:  152
Reverse of input number is: 251```

### Reverse Number In Java Using For Loop

In this method, we will use a for loop instead of a while loop.

```package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx2
{
public static void main(String[] args)
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number

//loop to find reverse number
for( ;num != 0;)
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
};

System.out.println("Reverse of input number is: "+reversenum);
}
}```

Output:

```Input your number and press enter: 159
Reverse of input number is: 951```

### Reverse Number In Java Using Recursion

This method divides a number by 10 and displays the remainder. It then calls itself, passing the quotient as a parameter. This process continues until the number is in single digits. The last digit (which is also the first digit of the original number) is displayed, and then the recursion ends.

```package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx3
{
public static void main(String[] args)
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
System.out.println ("Reversed Number: " + getReverse(num, reversenum));
}
static int getReverse (int num, int rev)
{
if (num == 0)
return rev;

int rem = num % 10;
rev = rev * 10 + rem;

return getReverse (num / 10, rev);
}
}```

Output:

```Input your number and press enter: 123
Reversed Number: 321```

You can do the same regression using the same recursive function with a single parameter like below.

```package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class ReverseNumberEx4
{
public static void main(String[] args)
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
getReverse(num);
}
static void getReverse (int num)
{
if (num == 0)
return;

int rem = num % 10;
System.out.print (rem);

getReverse (num / 10);
}
}```

Output:

```Input your number and press enter: 789
987```

### Reverse Number Without Using Modulus Operator

```package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms_103_ReverseNumberWithoutModulus
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

// Input: Get the number from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Call the reverseNumber method and print the result
System.out.println("Reversed number: " + reverseNumber(number));
}

// Method to reverse a number without using the modulus operator
private static int reverseNumber(int num) {
int reversed = 0;

// Iterate until the input number becomes 0
while (num != 0) {
// Extract the last digit
int digit = num - (num / 10) * 10;

// Update the reversed number by adding the extracted digit
reversed = reversed * 10 + digit;

// Remove the last digit from the input number
num = num / 10;
}

return reversed;
}
}```

Output:

```Enter a number: 12345
Reversed number: 54321```

Conclusion:

In this blog post, we have tried to share various possible ways to Reverse Number In Java. If you found its helpful, please share it with your friends and colleagues. If you have any questions or feedback, drop a comment below.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.