Friday, September 21, 2018

Java Program to Reverse a Number

How to reverse a number in Java without using any API is asked in many interviews in order to check the logic. Though how to reverse a string program is applicable for numbers also by treating the numbers as String but in some interviews it is specifically asked that it has to be a number and then you need to reverse it without using any in-built method.

Reversing number in Java can be done in two ways

  • By iterating through digits of the number and using mathematical operators like divide and multiply.
  • Using recursive function.

Iterative logic for reversing a number

When iterating through each digit of the number idea is to divide the given number by 10 and adding that remainder to an integer (which is initially initialized to 0) also multiply that integer by 10 in each iteration. That is required to move place values in the reversed number. Also divide the original number by 10 to get the quotient.

As example – If original number is 189 then first iteration will give remainder as 9 and quotient as 18. In the second iteration remainder will be 8 and quotient 1. And every time it is also multiplied by 10 for place value. Thus after second iteration it will be (9 * 10) + 8 = 98. Same for third iteration where remainder will be 1 and quotient 0. Thus making it (98 * 10) + 1 = 981. Which is the reversed number.

Recursive logic for reversing number

In recursive method you call the same method with one less digit in every recursive call. That is done by dividing the number by 10. You have to print modulo division in every recursive call. Using recursion this way will also print the zeroes which the iterative logic will not do. Meaning using the first method will give you 2 if input is 200. Whereas recursive method will give 002.

Java program to reverse a number

import java.util.Scanner;

public class ReverseNumber {

 public static void main(String[] args) {
  System.out.println("Please enter a number : ");
  Scanner sc = new Scanner(System.in);
  int scanInput = sc.nextInt();
  // Using recursion
  reverseRec(scanInput);
  System.out.println();
  System.out.println("------------------");
  // Using while loop
  reverseNum(scanInput);
 }
 
 // Method for reversing number using recursion
 public static void reverseRec(int num){
  //System.out.println("num" + num);
  if(num == 0)
   return;
  System.out.print(num % 10);
  reverseRec(num/10);
 }
 
 // Non-recursive method for reversing number  
 public static void reverseNum(int num){
  int reversedNum = 0;
  int mod = 0;
  while(num != 0){
   mod = num % 10;
   reversedNum = (reversedNum * 10) + mod;
   num = num/10;
  }
  System.out.println("reversedNum -- " + reversedNum);
 }
}

Output

Please enter a number : 
91346
64319
------------------
reversedNum -- 64319

That's all for this topic Java Program to Reverse a Number. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. Displaying Prime Numbers - Java Program
  2. Converting float to int - Java program
  3. Arrange Non-Negative Integers to Form Largest Number - Java Program
  4. How to Run Threads in Sequence - Java Program
  5. Print Odd-Even Numbers Using Threads And wait-notify

You may also like-

  1. How to Untar a File - Java Program
  2. How to Append to a File in Java
  3. String in Java
  4. Constructor chaining in Java
  5. Difference between Encapsulation and Abstraction in Java
  6. Difference between HashMap and ConcurrentHashMap in Java
  7. Difference Between Comparable and Comparator in Java
  8. Java Stream API Tutorial

1 comment:

  1. Thanks for This Code for Reverse a number Program in Java It's helpful for write any reverse number program in Java.

    ReplyDelete