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.
- Was it a car or a cat I saw
Example 1: Java Program for Palindrome Number
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
- Declare num,rem,sum and temp as integer type and initialize sum to zero .
- Ask user to enter a number.
- Store the value in num variable.
- Now we copy the value of the num in temp which is a temporary variable as we will manipulate the value of num.
- Now we will use WHILE loop.We divide the number by 10, until it is greater than 0. And do
- Then Remainder is stored in rem .
- Then Sum will be (sum*10)+rem .
- Then Number is divided by 10, num=num/10
After 5th step we have the reverse of the input.
- Now we compare the original number which is stored in ‘temp’ with reversed number-
- If it is equal to original one, then print that Number is a Palindrome.
- If not, then print that Number is not Plaindrome.
Program to Check for Palindrome Number
public static void main(String args)
Scanner sc=new Scanner(System.in);
System.out.println("Enter number ");
rem=num%10; //rem stores the remainder
System.out.println("\n-->It is a Palindrome Number<--\n");
System.out.println("\n-->It is Not a Palindrome Number<--\n");
Example 2: String Palindrome Program in Java
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
- Ask user to input a string using System.out.println("Input a string");
- Store the string in inputstring variable which is of string type.
- Declare variable length, begin, middle, end and i of integer type.
- Initialize variable length with the length of the string which is found by using the length() function.
- Initialize begin=0 , end=length-1 and middle=(begin+end)/2 .
- Using for loop, we will take control till the middle of the string and do
- Compare If the current letter stored in
- Increment begin with 1 and Decrement end with 1.
- Break the loop and exit
- Compare If the current letter stored in begin and end
i is equals to
middle+1 , then
- Display message that String is a Palindrome
- Display message that String is not a Palindrome.
String Palindrome Program in Java
public static void main(String args)
int i, begin, end, middle,length;
Scanner in = new Scanner(System.in);
System.out.println("Input a string");
inputstring = in.nextLine();
length = inputstring.length();
begin = 0;
end = length - 1;
middle = (begin + end)/2;
for (i = begin; i <= middle; i++)
if (inputstring.charAt(begin) == inputstring.charAt(end) || inputstring.charAt(begin) == ' ' || inputstring.charAt(end) == ' ' )
if (i == middle + 1)
System.out.println("\n-->It is a Palindrome String<--\n");
System.out.println("\n-->It is Not a Palindrome String<--\n");
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.