How to Reverse a Number in Java
In this section, we will learn how to reverse a number in Java using while loop, for loop and recursion.
To reverse a number, follow the steps given below:
- First, we find the remainder of the given number by using the modulo (%) operator.
- Multiply the variable reverse by 10 and add the remainder into it.
- Divide the number by 10.
Repeat the above steps until the number becomes 0.
There are three ways to reverse a number in Java:
- Reverse a number using while loop
- Reverse a number using for loop
- Reverse a number using recursion
Let’s apply the above steps in an example.
Example
Suppose, we want to reverse the number 1234.
In this example, we have taken three variables named number (the number to be reversed), remainder (stores the remainder), reverse (stores the reverse number) initialized 0.
Iteration 1:
remainder = 1234 % 10 = 4
reverse = 0 * 10 + 4 = 0 + 4 = 4
number = 1234 / 10 = 123
Now the value of the number and reverse variable is 123 and 4, respectively.
Iteration 2:
remainder = 123 % 10 = 3
reverse = 4 * 10 + 3 = 40 + 3 = 43
number = 123 / 10 = 12
Now the value of the number and reverse variable is 12 and 43, respectively.
Iteration 3:
remainder = 12 % 10 = 2
reverse = 43 * 10 + 2 = 430 + 2 = 432
number = 12 / 10 = 1
Now the value of the number and reverse variable is 1 and 432, respectively.
Iteration 4:
remainder = 1 % 10 = 1
reverse = 432 * 10 + 1 = 4320 + 1 = 4321
number = 1 / 10 = 0
Now the variable number become 0. Hence, we get the reverse number 4321.
Let’s implement the above logic in a Java program.
Reverse a number using while loop
ReverseNumberExample1.java
Output
The reverse of the given number is: 456789
Reverse a number using for loop
In the following program, we have replaced the while loop by a for loop. It also removes the last digit of the number, after each iteration. When the condition, number!=0 becomes false, the loop exits and we get the reversed number.
ReverseNumberExample2.java
Output
The reverse of the given number is: 654321
In the above program, we can also write the for loop as follows:
Reverse a number using recursion
ReverseNumberExample3.java
Output 1:
Enter the number that you want to reverse: 9 The reverse of the given number is: 9
Output 2:
Enter the number that you want to reverse: 7654123 The reverse of the given number is: 3214567
The following program reverses both numbers, positive and negative. When we enter a number, it first checks the number is positive or negative. If the number is negative, it converts the number into positive by multiplying -1. After that, it performs the same steps (as we have performed in the above programs) to reverse a number. At last, again it checks the number is negative or positive. To make the number negative, it again multiplies the reverse number by -1.
ReverseNumberExample4.java
Output 1:
Enter the number that you want to reverse: -98765 The reverse of the given number is: -56789
Output 2:
Enter the number that you want to reverse: 321987 The reverse of the given number is: 789123