How to check if the given number is palindrome in Java

Carvia Tech | May 18, 2019 | 2 min read | 33 views | Java Coding Challenges


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.

So our approach will be quite simple:

  1. Reverse the input number

  2. Check if the input and its reversed counterpart are same or 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.

JUnit Testcase
public void test1() {
    Palindrome palindromeChecker = new Palindrome();
    assertThat(palindromeChecker.isPalindrome(202), equalTo(true));
    assertThat(palindromeChecker.isPalindrome(10801), equalTo(true));
    assertThat(palindromeChecker.isPalindrome(48084), equalTo(true));
    assertThat(palindromeChecker.isPalindrome(1), equalTo(true));
    assertThat(palindromeChecker.isPalindrome(12), equalTo(false));

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

Java Coding Challenges:
  1. How will you check if a given sentence is a pangram
  2. Calculate factorial of a number in Java using recursion
  3. Write a program to reverse a string using recursion in Java
  4. Check a number is Prime: Java Coding Problem
  5. Create anagram buckets from a given input array of words
  6. Find two numbers of which the product is maximum in an array
  7. Java program to check if two strings are anagrams
See all articles in Java Coding Challenges
Top articles in this category:
  1. Top 50 SDET Java Programming Interview Questions & Answers
  2. Check whether given number is even or odd
  3. Check if the given number is Armstrong Number in Java
  4. Check a number is Prime: Java Coding Problem
  5. How to reverse a number in Java
  6. Calculate Fibonacci Series in Java
  7. Calculate factorial of a number in Java using recursion

Find more on this topic:
SDET Interviews image
SDET Interviews

End to end automation testing using Selenium Web Driver, Rest Assured, JMeter, Junit, TestNG etc.

Last updated 1 month ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info