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 on school, colleges and training institutes. I personally feel java program to reverse number is good programming exercise for some one who is just started learning programming in Java or any other programming language because of its simplicity and 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 remainder.
If you are looking for theoretical java interview question which involves understanding of concept rather than programming then you may like Why multiple inheritance is not supported in Java, why wait and notify are defined in object class and Why 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 length of linked list using iteration and recursion and design and code for vending machine in Java which accepts per-defined coin and return per-defined product handling all practical condition like returning change, returning money if product is not there etc
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 number with one digit less than original one, same time reverse number got that last digit as there first digit. Worth noting point is 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 Java program. This simple Java program can also be used to check if a number is palindrome or not. As a palindrome is a number whose reverse is equal to original number. If you like to practices recursion than try coming out of recursive way of reverse a number in Java, to get your hand going with recursion you can check rather intuitive example of calculating factorial of a number in Java using recursion.
Other Java Programming tutorials you may like
Tidak ada komentar:
Posting Komentar