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
Upasana | July 24, 2020 | 2 min read | 1,394 views | Java Coding Challenges
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:
- 
Get the input number and copy it in temporary variable 
- 
Reverse the original number 
- 
Compare if the temporary number is same as the reversed number 
- 
If both are same then input is a palindrome else not 
Let’s write the implementation in java.
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:
- 
check a palindrome number 
- 
check a non-palindrome number 
- 
check zero as the input 
- 
check for negative numbers 
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.assertThrows
class PalindromeNumberCheckerTest {
    @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
https://github.com/cancerian0684/junit5-gradle-kotlin-sample
Java Coding Challenges:
- Reverse position of words in a string using recursion
- Check if the given string is palindrome
- Find two numbers of which the product is maximum in an array
- Prime number checker in Java
- Create anagram buckets from a given input array of words
- Anagrams string checker in Java
- Reverse a string using recursion in Java
Top articles in this category:
- Check if the given string is palindrome
- 50 Java Interview Questions for SDET Automation Engineer
- Java Coding Problems for SDET Automation Engineer
- Anagrams string checker in Java
- Junit interview questions for SDET automation engineer
- Rest Assured API Testing Interview Questions
- Prime number checker in Java
 
                        