Updated March 17, 2023
Introduction to Palindrome in Java
String or a number is said to be a palindrome if it remains the same even after it is reversed. For example, ‘MADAM’ is a palindrome string since it is spelled ‘MADAM’ even if it is reversed. But in the case of ‘LUCKY’, this string is not palindrome as it is ‘YKCUL’ when it is reversed. Some of the palindrome numbers are 365563, 48984, 12321, 171, 88, 90009, 343, and some of the palindrome strings are MADAM, MALAYALAM, LOL, DAD, MOM, C++&++C, etc. Let us see the logic and implementation of palindrome in the following sections. In this topic, we are going to learn about Palindrome in Java.
Logic Behind Palindrome in Java
In order to check whether a number is a palindrome, the following algorithm can be used.
- Take an input string or number that has to be checked whether it is a palindrome or not.
For example, let us take the number 353 as input.
- Take the input number and copy it into a temp variable
353-> temp
- Reverse it using for, while or any method of your choice.
Reversednumber: rev=353
- Compare the input number and reversed number.
If they are the same, then the number is said to be a palindrome number.
Else, the number is not a palindrome number.
i.e.
If(inputnum==rev)
{ then palindrome }
Else not palindrome
How to Test Palindrome using Various Methods?
There are several methods in order to check whether the given input number is a palindrome or not.
- For Loop
- While Loop
- Library Method(for strings)
Let us look into each of them in detail:
1. Program to check palindrome number using for loop
Code:
//Java program to check whether a String is a Palindrome or not using For Loop
import java.util.*;
public class PalindromeNumberExample {
//main method
public static void main(String[] args) {
int r=0 ; //reversed Integer
int rem, num; //remainder and original number
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
for( ;num != 0; num /= 10 )
{
rem = num % 10; // find the modulus of the number when divided by 10
r = r * 10 + rem;
}
//check whether the original and reversed numbers are equal
if (temp == r)
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
}
else
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
}
}
}
Output 1:
Here, as 353 is the same when reversed, it is considered as a palindrome.
Output 2:
Here, as 234 remains not the same when reversed, it is not considered a palindrome.
2. Program to check palindrome number using While loop
Code:
//Java program to check whether a number is a Palindrome or not using While Loop
import java.util.*;
public class PalindromeNumberExample {
public static void main(String[] args) {
int r=0, rem, num;
Scanner s = new Scanner(System.in);
System.out.print("Enter number that has to be checked:");
num = s.nextInt();
//Store the number in a temporary variable
int temp = num;
//loop to find the reverse of a number
while( num != 0 )
{
rem= num % 10;
r= r * 10 + rem;
num=num/10;
}
//check whether the original and reversed numbers are equal
if (temp == r)
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are equal " + temp + " is a palindrome number");
}
else
{
System.out.println(temp + " is input number");
System.out.println(r + " is the reversed number");
System.out.println("Since they are not equal " + temp + " is not a palindrome number");
}
}
}
Output 1:
Output 2:
3. Program to check palindrome number using library method (for strings)
Code:
//Java program to check whether a String is a Palindrome or not using Library method
import java.util.*;
public class PalindromeNumberExample {
//Function to check whether the string is palindrome or not
public static void PalindromeCheck(String str)
{
// reverse the input String
String rev = new StringBuffer(str).reverse().toString();
// checks whether the string is palindrome or not
if (str.equals(rev))
{
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ str +" is a palindrome");
}
else
{
System.out.println("input string is :" + str);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ str +" is not a palindrome");
}
}
public static void main (String[] args)
{
PalindromeCheck("MALAYALAM");
}
}
Output:
Here, the input string is passed in the program itself.
To check whether a string is a palindrome, the following program is also used.
Code:
//Java program to check whether a String is a Palindrome or not
import java.util.*;
public class PalindromeNumberExample {
public static void main(String args[])
{
String st, rev = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string that has to be checked:");
st = sc.nextLine();
int len = st.length(); //length of the string
for ( int i = len- 1; i >= 0; i-- )
rev = rev + st.charAt(i);
if (st.equals(rev))
{
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are equal, "+ st +" is a palindrome");
}
else
{
System.out.println("input string is :" + st);
System.out.println("Reversed string is :" + rev);
System.out.println("Since the input and reversed string are not equal, "+ st +" is not a palindrome");
}
}
}
Output:
Conclusion
A number is said to be palindrome if it remains the same even when it is reversed. A palindrome can be checked in strings also. Some of the palindrome numbers and strings are MOM, MALAYALAM, DAD, LOL, 232, 1331, etc. In this document, several aspects of Palindrome are covered, such as algorithm, methods, implementation, etc.
Recommended Articles
This is a guide to Palindrome in Java. Here we discuss how to test palindrome using various methods with the sample output. You may also have a look at the following articles to learn more –