Sunday, September 24, 2023

How to check if a number is a palindrome or not in Java - Example

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 the number itself is equal to the reverse of number e.g. 313 is a palindrome because the reverse of this number is also 313. On the other hand, 123 is not a palindrome because the 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 a number in Java.

Since in most of the interviews, 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 programmers solving this question by first converting an integer to String and then reversing String using the reverse() method of StringBuffer and then converting String back to Integer, which is not a correct way because you are using Java API. 

Some programmers may think that this is just a trivial programming exercise but it’s not. Questions like this or the Fibonacci series using recursion can easily separate programmers who can code and who can’t. So it’s always in the best interest to keep doing programming exercises and developing logic.




Java program to check if a number is palindrome or not

How to check if a number is palindrome or not in Java with ExampleHere is a simple Java program that finds if a number is a palindrome or not. This program does not use any API method instead uses division and remainder operator of Java programming language to determine if the number is palindrome or not.

Programming logic to reverse a number is encapsulate in reverse() method and isPalindrome(int number) reuses that logic to test if a number is a 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, a palindrome is a classical coding question and should not be missed during preparation.


Other Java programming exercises for practice

And lastly one question for you? Which one is your favorite Java programming exercise? Palindrome, Prime number, Fibonacci, Factorial or this one? 

6 comments :

Anonymous said...

I think is better to write one line:
return number == reverse(number);
instead of:
if(number == reverse(number)){
return true;
}
return false;

Anonymous said...

Check any number is palindrome or not using below program:


public class ReverseNumber {

//int numb;
public static void revere(int number ){
String input= String.valueOf(number);
StringBuffer buffer=new StringBuffer(input);
buffer.reverse();
String reversed=buffer.toString();
int rev=Integer.parseInt(reversed);
System.out.println(rev);


}
public static void main(String[] args) {
revere(4545);

}

}

javin paul said...

@Anonymous, If use of StringBuffer reverse() method is permitted then that's good solution but mostly you are asked to reverse the String without using this method, but yes, something to know about.

Anonymous said...

write by looping statement in javascript

Anonymous said...

There is no need to reverse or process all the digits in case of polyndrome, as in the following C function;

int is_polyndrome(int num)
{
int p = 0, d;

while (num > 0) {
d = num % 10;
p = p * 10 + d;
num /= 10;
if (num == p)
return 1; /* half the number of digits point */
}

return 0;
}

Chakotay said...

When the integer length is odd(131, 12321 ...etc.), the program will answer 0, which is not correct.

Post a Comment