Frequently Asked Java Program 01: Java Program to Check If a Given Number is Palindrome

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to check if a given number is palindrome.

Palindrome program is a frequently asked programming interview question to both freshers or experienced in interviews. We will going to learn that in this post.

Let’s start with basics first. We must be aware what is Palindrome.

What is palindrome?

We read a word or group of words from left to right. If we can read them from right to left same as we read left to right , is called a palindrome.

Examples: “Refer” ,  “level” , “Madam” , “Nurses run” , 1991 are palindrome.

A number can also be a palindrome number as shown above in example. In this post, we will be learn about a palindrome number. In next post, we will learn about string palindrome.

A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed.

For example: 16461.

Logic to find out a numeral palindrome:

    1. We need to extract last digit from input number and keep it at first position for a new number.
    2. Extract the second last digit and put in the next position of new number.
    3. We can achieve that using multiple ways like reverse traversing or charAt methods etc. Here we will see include some number concepts.
    4. We can extract last digit of a number(remainder) if we divide number by 10.

For examples:
a. 13/10 : Remainder= 3 which is last digit in number 13.
b. 100/10 : Remainder= 0 which is last digit in number 10.

5. New number which is actually reverse number, will be formed by formula “(reverseNumber*10) + remainder”.
6. Now divide the original number by 10 to remove last digit of it.
7. Repeat above steps till original number is less than zero.

Java Program:

Output:

Please give the input number to check palindrome:
-10
Number to be checked for palindrome: -10
Negative number.Enter positive number.
=========================================================
Please give the input number to check palindrome:
5
Number to be checked for palindrome: 5
5 is palindrome as it is single digit number.
=========================================================
Please give the input number to check palindrome:
16461
Number to be checked for palindrome: 16461
Input By User:16461
Reverse number:16461
16461 is a Palindrome Number
=========================================================
Please give the input number to check palindrome:
87342
Number to be checked for palindrome: 87342
Input By User:87342
Reverse number:24378
87342 is not a Palindrome Number

You can run above program for multiple inputs and if it fails for any condition, let me know.

#HappyCoding

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning