How to reverse a number in Java without using any API or write a simple Java program to reverse a number is common programming questions asked on fresher level software engineer interviews. Reversing a number is also popular homework question on many Java programming courses in schools, colleges, and training institutes. I personally feel java program to reverse number is good programming exercise for someone who is just started learning to program in Java or any other programming language because of its simplicity and a little bit of trickiness which shows how to use operators for programming purposes rather than arithmetic purpose.
In last couple of Java programming tutorial, we have seen some basic programming exercises like how to reverse a string in Java using recursion and how to check if a number is prime or not, while in this Java tutorial we will see a simple example of Java program to reverse number by just using basic Java operators like division operator(/) and remainder operator(%). division operator returns quotient while modulus or remainder operator % returns the remainder.
If you are looking for a theoretical java interview question that involves an understanding of concept rather than programming then you may like Why multiple inheritances is not supported in Java, why to wait and notify are defined in object class, and Why the main method is public static in Java.
But I would say you better horn your programming skills as well by programming Simple programs than moving to tougher one which involves data-structure and designs like how to find the length of linked list using iteration and recursion and design and code for the vending machine in Java which accepts pre-defined coin and returns pre-defined product handling all practical condition like returning the change, returning money if the product is not there, etc
How to reverse a number in Java – Example
Here is a complete code example of a reversing number in Java without using any API method. This simple Java program just uses basic programming concepts like loops and operators to reverse numbers. After each iteration, you have a number with one digit less than the original one, same time reverse number got that last digit as there first digit. Worth noting point is a multiplication of 10 which is required to move places in decimal numbers.
import java.util.Scanner;
/**
* Simple Java program to reverse a number in Java using loop and operator
* This program also shows example of using division operator(/) and Remainder Operator(%)
*/
public class ReverseNumberExample {
public static void main(String args[]) {
//input number to reverse
System.out.println("Please enter number to be reversed using Java program: ");
int number = new Scanner(System.in).nextInt();
int reverse = reverse(number);
System.out.println("Reverse of number: " + number + " is " + reverse(number));
}
/*
* reverse a number in Java using iteration
* @return reverse of number
*/
public static int reverse(int number){
int reverse = 0;
int remainder = 0;
do{
remainder = number%10;
reverse = reverse*10 + remainder;
number = number/10;
}while(number > 0);
return reverse;
}
}
Output:
Please enter number to be reversed using Java program:
1234
Reverse of number: 1234 is 4321
That’s all on how to reverse a number in a Java program. This simple Java program can also be used to check if a number is a palindrome or not. As a palindrome is a number whose reverse is equal to the original number. If you like to practices recursion than try coming out of the recursive way of reverse a number in Java, to get your hand going with recursion you can check rather an intuitive example of calculating factorial of a number in Java using recursion.
Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS