How to check if the given number is palindrome in Java

Carvia Tech | September 09, 2019 | 2 min read | 382 views | Java Coding Challenges


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

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

You can download sample project from Github

https://github.com/cancerian0684/junit5-gradle-kotlin-sample


Java Coding Challenges:
  1. Find two numbers of which the product is maximum in an array
  2. How will you check if a given sentence is a pangram
  3. How to reverse a number in Java
  4. Check if the given number is Armstrong Number in Java
  5. Check a number is Prime: Java Coding Problem
  6. Calculate factorial of a number in Java using recursion
  7. Write a program to reverse a string using recursion in Java
See all articles in Java Coding Challenges
Top articles in this category:
  1. Top 15 coding problems for SDET Java Interviews
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. SDET: JUnit interview questions for automation engineer
  4. SDET: Rest Assured Interview Questions
  5. Check if the given string is palindrome
  6. Check whether given number is even or odd
  7. Check if the given number is Armstrong Number in Java


Find more on this topic:
SDET Interviews image
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:

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