Program to Reverse a Number in Java

In this post, we will develop a program to reverse an integer number in Java. We will take input from the end-user and display the reverse of the number.

To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero. To remove the last digit the modulus operator (%) will be helpful for us, and to remove the last digit division operator (/) can be used. To add the last digit first we need to multiply the previous value by 10.

Procedure to find reverse of a number,

1) Take a number
2) Declare a temporary variable and initialize it with 0
3) find the last digit of the number
4) multiply the temporary variable by 10
5) add that last digit to the temporary variable
6) remove the last digit of the number.
7) Repeat this process from 3 to 6 until the number becomes 0.

Java method to find reverse of a number

public static int findReverse(int num) {

   // variable to store last digit
   int lastDigit ;
   // declare a temporary variable and
   // initialize it with 0
   int reverse = 0;

   // loop to repeat the process
   while(num != 0) {

      // find last digit of number
      lastDigit = num % 10;

      // multiply temporary variable by 10
      reverse = reverse * 10;

      // add last digit to reverse variable
      reverse = reverse + lastDigit;

      // remove last digit
      num = num / 10;
   }

   return reverse;
}

The above Java method to find the reverse of a number is by using the while loop. While loop in Java is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

In the above Java method, inside while loop, the first three lines of code can be written in one line as,

reverse = (reverse*10) + (num % 10);

Program to reverse a number in Java

import java.util.Scanner;

public class ReverseNumber {

   public static int findReverse(int num) {

      // declare a temporary variable and
      // initialize it with 0
      int reverse = 0;

      while(num != 0) {
         // find last digit of number
         // and add it to temporary variable
         reverse = (reverse*10) + (num % 10);

         // remove last digit
         num = num / 10;
      }

      return reverse;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;
      int result = 0;

      // create Scanner object
      Scanner scan = new Scanner(System.in);

      // take input
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // find the reverse of the number 
      result = findReverse(number);

      // display the result
      System.out.print("The reverse of "+number+
              " is = "+ result);

      // close Scanner class object
      scan.close();
   }
}

Time Complexity of this program is O(log(n))

Output for different test-cases:-

Enter an integer number:: 12345
The reverse of 12345 is = 54321

Enter an integer number:: 987654
The reverse of 987654 is = 456789

We can also use for loop instead of the while loop to find the reverse of the number,

public static int findReverse(int num){

   int reverse;
   for(reverse = 0; num != 0; num /= 10) {
       reverse = (reverse*10) + (num % 10);
   }
   return reverse;

}

Recursion technique to find the reverse

The base case for finding the reverse of the number,

when number == 0
then return 0;

The general case for finding the reverse of the number,

reverse = (reverse*10) + (num%10);
return findReverse(num/10);
import java.util.Scanner;

public class ReverseNumber {

   // variable to store reverse value
   static int reverse;

   // method to find reverse of number
   // using recursion technique
   public static int findReverse(int num) {

      if(num==0) return 0; //base case

      else { // general case
         reverse = (reverse*10) + (num%10);
         return findReverse(num/10);
      }
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // create Scanner object
      Scanner scan = new Scanner(System.in);

      // take input
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // find the reverse of the number 
      findReverse(number);

      // display the result
      System.out.print("The reverse of "+number+
             " is = "+ reverse);

      // close Scanner object
      scan.close();
   }
}

The palindrome number program in Java is based on the finding reverse of the integer number.

If the reverse of the number is equal to the same number then the number is called a palindrome number. Example of palindrome number are:- 5225, 123454321 e.t.c

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *