Java Program for Palindrome Number & Palindrome String

In this article we will be seeing two Java program for palindrome, Palindrome is a number, word or  phrase or other sequence of character which is same as original after its reversal.

For example:

  • 121
  • 56765
  • LEVEL
  • REFER
  • Was it a car or a cat I saw

Must Check: What is JAVA And How To Write & Run Your First Program

Example 1: Java Program for Palindrome Number

palindrome-number-program-in-java

Logic for the Solution

Basically, we will be taking numerical input from the user in the variable of a particular data type. Then we will reverse the input and will compare it with our originally stored number. If it is the same as the original then its a Palindrome otherwise, not.

These are the steps that we will follow

  1. Declare num,rem,sum  and temp  as integer type and initialize sum  to  zero .
  2. Ask user to enter a number.
  3. Store the value in num  variable.
  4. Now we copy the value of the num  in temp  which is a temporary variable as we will manipulate the value of num.
  5. Now we will use WHILE loop.We divide the number by 10, until it is greater than 0. And do
    1. Then Remainder is stored in rem .
    2. Then Sum will be (sum*10)+rem .
    3. Then Number is divided by 10, num=num/10

After 5th step we have the reverse of the input.

  1. Now we compare the original number which is stored in ‘temp’ with reversed number-
    1. If it is equal to original one, then print that Number is a Palindrome.
    2. If not, then print that Number is not Plaindrome.

Also Check: JAVA Programs – 3 Ways of Assigning Values to Variables

Program to Check for Palindrome Number

Example 2: String Palindrome Program in Java

palindrome-program-in-java-using-string

Logic of the solution:

Basically, we will take an input from the user and store it in a String type variable. There will be two checkpoints of the string ‘begin’ and ‘end ‘. ‘Begin’ will store the first letter and ‘end’ will be storing last letter of the string.

We will compare letters in ‘begin’ with ‘end’ at first, if they are same then we will continue, otherwise we will break from the loop and display a message that String is not Palindrome. If all the letters are same and we reach the middle of the string, then control will exit from the loop and display a message that Entered String is a Palindrome.

We will take the following steps

  1. Ask user to input a string using System.out.println("Input a string");
  2. Store the string in inputstring  variable which is of string type.
  3. Declare variable length, begin, middle, end  and i  of integer type.
  4. Initialize variable length  with the length of the string which is found by using the length()  function.
  5. Initialize begin=0 , end=length-1  and middle=(begin+end)/2 .
  6. Using for loop, we will take control till the middle of the string and do
    1. Compare If the current letter stored in begin  and end
      1. Increment begin  with 1 and Decrement end  with 1.
    2. Else
      1. Break the loop and exit
  7. If i  is equals to middle+1 , then
    1. Display message that String is a Palindrome
  8. Else
    1. Display message that String is not a Palindrome.

String Palindrome Program in Java

You can run all these programs through the CMD or BlueJ as per your need, you can also create the graphical version of the program by using the Java Swing Examples as reference. Let us know if you face any problem with the program by leaving a comment below, and don’t forget to share this program with your friends and classmates.

February 9th, 2016

1 Responses on Java Program for Palindrome Number & Palindrome String|

  1. Hi Satya,
    Will try this code for sure.
    Thanks for sharing.

Leave a Message

Your email address will not be published. Required fields are marked *

CommentLuv badge

Pin It on Pinterest