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.

This image has an empty alt attribute; its file name is how-to-reverse-a-number-in-java.png

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:number = 1234
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:number = 123
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:number = 12
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:number = 1
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

  1. public class ReverseNumberExample1   
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. int number = 987654, reverse = 0;  
  6. while(number != 0)   
  7. {  
  8. int remainder = number % 10;  
  9. reverse = reverse * 10 + remainder;  
  10. number = number/10;  
  11. }  
  12. System.out.println(“The reverse of the given number is: ” + reverse);  
  13. }  
  14. }  

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

  1. public class ReverseNumberExample2   
  2. {  
  3. public static void main(String[] args)   
  4. {  
  5. int number = 123456, reverse = 0;  
  6. //we have not mentioned the initialization part of the for loop  
  7. for( ;number != 0; number=number/10)   
  8. {  
  9. int remainder = number % 10;  
  10. reverse = reverse * 10 + remainder;  
  11. }  
  12. System.out.println(“The reverse of the given number is: ” + reverse);  
  13. }  
  14. }  

Output

The reverse of the given number is: 654321

In the above program, we can also write the for loop as follows:

  1. for(;number != 0;)   
  2. {  
  3. int remainder = number % 10;  
  4. reverse = reverse * 10 + remainder;  
  5. number=number/10;  
  6. }  

Reverse a number using recursion

ReverseNumberExample3.java

  1. import java.util.Scanner;  
  2. public class ReverseNumberExample3  
  3. {  
  4. //method for reverse a number  
  5. public static void reverseNumber(int number)   
  6. {  
  7. if (number < 10)   
  8. {  
  9. //prints the same number if the number is less than 10  
  10. System.out.println(number);  
  11. return;  
  12. }  
  13. else   
  14. {  
  15. System.out.print(number % 10);  
  16. reverseNumber(number/10);  
  17. }  
  18. }  
  19. public static void main(String args[])  
  20. {  
  21. System.out.print(“Enter the number that you want to reverse: “);  
  22. Scanner sc = new Scanner(System.in);  
  23. int num = sc.nextInt();  
  24. System.out.print(“The reverse of the given number is: “);  
  25. //method calling  
  26. reverseNumber(num);  
  27. }  
  28. }  

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

  1. import java.util.*;  
  2. public class ReverseNumberExample4  
  3. {   
  4. public static void main(String args[])   
  5. {  
  6. System.out.print(“Enter the number that you want to reverse: “);  
  7. Scanner sc = new Scanner(System.in);  
  8. int n = sc.nextInt();  
  9. int reverse_number = reverseNumber(n);   
  10. System.out.println(“The reverse of the given number is: “+reverse_number);   
  11. }  
  12. //method to reverse a number  
  13. public static int reverseNumber(int number)   
  14. {  
  15. boolean isNoNegative = number < 0 ? true : false;   
  16. if(isNoNegative)  
  17. {  
  18. number = number * -1; //makes the number positive if the given number is negative  
  19. }   
  20. int reverse = 0;   
  21. int lastDigit = 0;   
  22. while (number >= 1)   
  23. {  
  24. lastDigit = number % 10; // gives the last digit of the number  
  25. reverse = reverse * 10 + lastDigit;   
  26. number = number / 10; // removes the last digit of the number  
  27. }   
  28. //makes the number negative   
  29. return isNoNegative == true? reverse*-1 : reverse;   
  30. }  
  31. }  

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

Leave a Comment