Pages

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 questions on many Java programming courses in school, 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 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 modules or remainder operator % returns the remainder.

How to reverse number in Java - Example

Here is complete code example of reversing number in Java without using any API method. This simple Java program just uses basic programming concept like loops and operators to reverse number. 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.

importjava.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(%) */publicclass ReverseNumberExample {

publicstaticvoid main(String args[]){//input number to reverseSystem.out.println("Please enter number to be reversed using Java program: ");int number = newScanner(System.in).nextInt();

Output:Please enter number to be reversed using Java program:1234Reverse of number: 1234 is 4321

That's all on how to reverse a number in 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.

@Jan, you are right on your part, you can reverse number by treating them as String in Java but this programming exercise is meant to do it without any API method and with just arithmetic operator in order to apply some kind of logic but no doubt it can be done by converting String to integer and using your solution.

Yeah, it's going to be tough to write something _without_ touching the API at all.In your example, just reading in the String to be converted to an int and then reversed requires the Scanner to read and the nextInt() to translate the next token to an int.

But, for this program, we would get wrong output if the input integer ends with "0". So instead of saving the remainder in to a int, instead we could use a string buffer and append it. Finally showing the stringbuffer as final ouput would be one option.

Hi Rob, I understand the question as 'literal' reversal of number in which case we would be missing the zero's at the end. If we just consider the actual integer value after reversal, what you said is right.