Java Program to Reverse an Integer Number - Example tutorial (original) (raw)
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.
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.
Other Java Programming tutorials you may like
And now is the quiz time, What is the time complexity of this algorithm to reverse a give integer number? can you improve this by using any data structure or different approach?