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 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 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 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 exercise and developing logic.

Also, basic knowledge of essential data structure and algorithms is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like

Since in most of the 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 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 exercise and developing logic.

Also, basic knowledge of essential data structure and algorithms is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like

**Data Structures and Algorithms: Deep Dive Using Java**on Udemy to improve your knowledge and algorithms skills.##
__Java program to check if a 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

*/

for(

*

* @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));

}

}

if(number == reverse(number)){

}

}

while(number != 0){

reverse = reverse*10 + number%10;

number = number/10;

}

}

}

Does number : 1 is a palindrome?

Does number : 20 is a palindrome?

Does number : 22 is a palindrome?

Does number : 102 is a palindrome?

Does number : 101 is a palindrome?

Does number : 1221 is a palindrome?

Does number : 13321 is a palindrome?

Does number : 13331 is a palindrome?

Does number : 0 is a palindrome?

Does number : 11 is a palindrome? true

}

}

**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.**Further Learning**

The Coding Interview Bootcamp: Algorithms + Data Structures

Data Structures and Algorithms: Deep Dive Using Java

Algorithms and Data Structures - Part 1 and 2

Other

**Java programming exercise**for practice
## 5 comments :

I think is better to write one line:

return number == reverse(number);

instead of:

if(number == reverse(number)){

return true;

}

return false;

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);

}

}

@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.

write by looping statement in javascript

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;

}

## Post a Comment