# Reverse Number Using Java, How To Reverse A Number In Java?, Recursion

In this java program example, we will learn to create a program to reverse a number using Java.

## Reverse Number In Java

reverse number is a number that is generated from another number. It is generated number like a palindrome of number means the digit of the first place will replace with the digit of the last place.
For example,number = 54321
reverse_Number = 12345

There are few steps to reverse a number:

1. STEP 1: Find the remainder of the given number using modular(%10) and store the remainder of, For example, the remainder of 54321 % 10 is 1.
2. STEP 2: Multiply the previous remainder with 10 (x10) then add the remainder with it. For Example, 0x10 + 1 = 1. Store the result in variable reverse_Number = 1.
3. STEP 3: Now, Divide the number by 10 as 54321/10 result will be 5432, and replace it with the original number = 5432.
4. STEP 4: Repeat STEP 1 to STEP 3 until the original number will be reversed.

Rerverse Number Program Java

Let’s take a look at each iteration in detail: Iteration depends on the digit available in number.

### Iteration 1

number = 54321 & reverse_Number = 0;

``````remainder = number % 10;       [  remainder = 54321 % 10 = 1 ]
reverse_Number =  reverse_Number x 10 + remainder;    [ reverse_Number = 0 x 10 + 1 = 1]
number =  number / 10 ;     [number = 54321 / 10 = 5432 ]``````

### Iteration 2

number = 5432 & reverse_Number = 1;

``````remainder = number % 10;       [  remainder = 5432 % 10 = 2 ]
reverse_Number =  reverse_Number x 10 + remainder;    [ reverse_Number = 1 x 10 + 2 = 12]
number =  number / 10 ;     [number = 5432 / 10 = 543 ]``````

### Iteration 3

number = 543 & reverse_Number = 12;

``````remainder = number % 10;       [  remainder = 543 % 10 = 3 ]
reverse_Number =  reverse_Number x 10 + remainder;    [ reverse_Number = 12 x 10 + 3 = 123]
number =  number / 10 ;     [number = 543 / 10 = 54 ]``````

### Iteration 4

number = 54 & reverse_Number = 123;

``````remainder = number % 10;       [  remainder = 54 % 10 = 4 ]
reverse_Number =  reverse_Number x 10 + remainder;    [ reverse_Number = 123 x 10 + 4 = 1234]
number =  number / 10 ;     [number = 54 / 10 = 5 ]``````

### Iteration 5

number = 5 & reverse_Number = 1234;

``````remainder = number % 10;       [  remainder = 5 % 10 = 5 ]
reverse_Number =  reverse_Number x 10 + remainder;    [ reverse_Number = 1234 x 10 + 5 = 12345]
number =  number / 10 ;     [number = 5 / 10 = 0 ]``````

## Java Program To Reverse Number

In Java, there are many ways to write a program to reverse a given number using Java. Let’s create a program in different ways as :

### Java Reverse Number using For Loop

In this program, the following program using For loop to reverse a number using Java. Program given below:

```
```

public class ReverseUsingForLoop{

public static void main(String []args){
int number = 54321;
int reverse_digit=0;
int remainder;

System.out.println("Before Reverse a Number is "+number);

for(;number!=0;){
remainder = number % 10;
reverse_digit = reverse_digit * 10 + remainder;
number = number / 10;

}
System.out.println("After Reverse a Number is "+reverse_digit);

}
}
```
```

#### Output :

``````Before Reverse a Number is 54321
After Reverse a Number is 12345``````

### Java Reverse Number using While Loop

In this program, we will use While loop to reverse a number using Java. Program given below:

```
```

public class ReverseUsingWhileLoop{

public static void main(String []args){
int number = 850603;
int reverse_digit=0;
int remainder;

System.out.println("Using While Loop - Before Reverse a Number is "+number);

while(number!=0){
remainder = number % 10;
reverse_digit = reverse_digit * 10 + remainder;
number = number / 10;

}
System.out.println("Using While Loop - After Reverse a Number is "+reverse_digit);

}
}
```
```

#### Output :

``````Using While Loop - Before Reverse a Number is 850603
Using While Loop - After Reverse a Number is 306058``````

### Java Reverse Number using Do-While Loop

In this program, we will use Do-While loop to reverse a number using Java. Java reverse program shown below:

```
```

public class ReverseUsingDoWhile{

public static void main(String []args){
int number = 850603;
int reverse_digit=0;
int remainder;

System.out.println("Using Do-While Loop - Before Reverse a Number is "+number);

do{
remainder = number % 10;
reverse_digit = reverse_digit * 10 + remainder;
number = number / 10;

}while(number!=0);
System.out.println("Using Do-While Loop - After Reverse a Number is "+reverse_digit);

}
}
```
```

#### Output :

``````Using Do-While Loop - Before Reverse a Number is 850603
Using Do-While Loop - After Reverse a Number is 306058``````

### Factorial using Recursion in Java

In this program, we will use recursion to reverse a number using Java. we use the recursive function to reverse the function until the base condition executed.

```
```

public class ReverseUsingRecursion{
static int reverse_digit=0;

public static int reverse(int number){
if(number <10){
reverse_digit = reverse_digit * 10 + number;
return  reverse_digit;
}

int remainder = number % 10;
reverse_digit = reverse_digit * 10 + remainder;
number = number / 10;
reverse(number);

return  reverse_digit;

}
public static void main(String []args){
int number = 850603;

System.out.println("Using Recursion - Before Reverse a Number is "+number);
int result = reverse(number);
System.out.println("Using Recursion - After Reverse a Number is "+result);

}
}
```
```

#### Output :

``````Using Recursion - Before Reverse a Number is 850603
Using Recursion - After Reverse a Number is 306058``````