Java Program to Reverse a Number is a classic interview question often asked to test a developer’s logical thinking and understanding of core programming concepts. Though how to reverse a string program is applicable for numbers also by treating the numbers as String, interviewers frequently emphasize reversing a number without converting it into a string or using any library functions. This ensures you demonstrate mastery of loops, recursion and mathematical operators.
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
In iterative approach, you repeatedly divide the number by 10 to extract its digits (obtained using the modulo operator %). You also need another int variable for storing the reversed number (initially initialized to 0). In each iteration, add the extracted digit to this variable, while also multiplying that variable by 10. Multiplication is required to move place values in the reversed number. Divide the original number by 10 too to get to the next digit.
For example, if original number is 189 then first iteration will give remainder as 9 and quotient as 18. Which stores the value (0 * 10) + 9 = 9 in the reverseNum variable. In the second iteration remainder will be 8 and quotient 1. After second iteration reverseNum variable will have value (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, the method calls itself with the number divided by 10 in each step, while printing or storing the remainder (num % 10). This technique naturally handles digit extraction but also preserves leading zeros. For example, input 200 will output 002 using recursion, whereas the iterative method would return 2.
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);
}
// Iterative 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
You may also like-
Thanks for This Code for Reverse a number Program in Java It's helpful for write any reverse number program in Java.
ReplyDelete