# How to check if the given number is palindrome in Java

Carvia Tech | September 09, 2019 | 2 min read | 464 views |

Palindrome

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10801.

## Palindrome checker in Java

In simple words, A Palindrome Number is a number that even when reversed is same as original number.

### Pseudo code

So our approach will be quite simple:

1. Get the input number and copy it in temporary variable

2. Reverse the original number

3. Compare if the temporary number is same as the reversed number

4. If both are same then input is a palindrome else not

Let’s write the implementation in java.

Palindrome Checker
``````public class Palindrome {

boolean isPalindrome(int input) {
int reversed = reverse(input);
return input == reversed;
}

private int reverse(int input) {
int lastDigit, sum = 0, temp;
temp = input;
while (temp > 0) {
lastDigit = temp % 10; (1)
sum = (sum * 10) + lastDigit;
temp = temp / 10;
}
return sum; (2)
}
}``````
 1 We are using remainder operator to get the last digit of number 2 sum is the reversed number

## JUnit testcase

We can write simple JUNIT testcase to assert that basic functionality of palindrome checker program.

Few testcases, that we can think of:

1. check a palindrome number

2. check a non-palindrome number

3. check zero as the input

4. check for negative numbers

JUnit Testcase
``````import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.assertThrows

@Test
fun `check a palindrome number`() {
val checker = PalindromeNumberChecker()
assertEquals(true, checker.isPalindrome(1001))
}

@Test
fun `check a non-palindrome number`() {
val checker = PalindromeNumberChecker()
assertEquals(false, checker.isPalindrome(12))
}

@Test
fun `check if zero is palindrome`() {
val checker = PalindromeNumberChecker()
assertEquals(true, checker.isPalindrome(0))
}

@Test
fun `check negative numbers`() {
val checker = PalindromeNumberChecker()
val exception = assertThrows<AssertionError> {
checker.isPalindrome(-100)
}
assertEquals("input must be positive number", exception.message)
}
}``````

That’s all!

 Here we are only considering integers for Palindrome, but the same logic can be applied to any other input type (word, phrase, etc)
 Did you know? The longest palindromic word in the Oxford English Dictionary is the onomatopoeic tattarrattat, coined by James Joyce in Ulysses (1922) for a knock on the door

If you are looking for more complex example, where we do the bit wise reversal of a number to check if it is palindrome or not, follow this article:

For String input, you can refer to Check if a given String is palindrome

You can download sample project from Github

###### Java Coding Challenges:
See all articles in Java Coding Challenges

##### SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

###### Recommended books for interview preparation:
Book you may be interested in..
##### ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel
Book you may be interested in..
##### ebook PDF - Cracking Spring Microservices Interviews for Java Developers
Book you may be interested in..