How to check if a number is a palindrome or not is a variant of popular String
interview question how to check if a String is a palindrome or not. A number
is said to be a palindrome if number itself is equal to reverse of number e.g.
313 is a palindrome because reverse of this number is also 313. On the other
hand 123 is not a palindrome because reverse of 123 is 321 which is not equal
to 123, i.e. original number. In order to check if a number is a palindrome or
not we can reuse the logic of How
to reverse number in Java. Since in most of interview, you are supposed to
solve this question without taking help from API i.e. only using basic
programming construct e.g. loop, conditional statement, variables, operators
and logic. I have also seen programmer solving this question by first
converting integer to String and than reversing String using reverse() method of StringBuffer and than
converting String back to Integer, which is not a correct way because you are
using Java API. Some programmer may think that this is just a trivial programming
exercise but it’s not. Questions like this or Fibonacci series using recursion can easily separate programmers who can code and who
can’t. So it’s always in best interest to keep doing programing exercise and
developing logic.
Java program to check if number is
palindrome or not
Here is a simple Java program which finds if a number is a palindrome or not. This program does not use any
API method instead it uses division and remainder operator of Java programming
language to determine if number is palindrome or not. Programming logic to reverse a number is encapsulate
in reverse() method and isPalindrome(int number) reuse that
logic to test if a number is palindrome or not.
import java.util.Scanner;
/**
* This Java program takes an input number from command line and integer
array
* and check
if number is palindrome or not. A number is called palindrome
* if number
is equal to reverse of number itself.
*
* @author Javin Paul
*/
public class
PalindromeTest {
public static void
main(String args[]){
Scanner scanner = new Scanner(System.in);
//int number =
scanner.nextInt();
int[] numbers = {1, 20, 22, 102, 101, 1221, 13321, 13331, 0, 11};
for(int
number: numbers){
System.out.println("Does number :
"
+ number +" is a palindrome? "
+ isPalindrome(number));
}
}
private static boolean
isPalindrome(int
number) {
if(number == reverse(number)){
return
true;
}
return false;
}
private static int
reverse(int
number){
int
reverse = 0;
while(number != 0){
reverse = reverse*10 + number%10;
number = number/10;
}
return
reverse;
}
}
Output
Does number : 1 is a palindrome? true
Does number : 20 is a palindrome? false
Does number : 22 is a palindrome? true
Does number : 102 is a palindrome? false
Does number : 101 is a palindrome? true
Does number : 1221 is a palindrome? true
Does number : 13321 is a palindrome? false
Does number : 13331 is a palindrome? true
Does number : 0 is a palindrome? true
Does number : 11 is a palindrome? true
That's all on how to check if a number is a palindrome or not. As
I said this is a good programming exercise particularly for beginners who have
just started learning Java programming language, as it teaches how to use
division and remainder operator in Java. Once again Fibonacci series,
Palindrome are classical coding question and should not be missed during
preparation.
Other Java programming exercise for practice
Tidak ada komentar:
Posting Komentar