Palindrome program in Java
In this post, you will learn different ways to check a perfect palindrome program using the Java programming language.
A palindrome is a number, string, or sequence that remains the same when it is reversed. A palindrome sequence can be a combination of digits or numbers, but it looks the same going forward and backward. It is generally used where recreational mathematics is applied, like puzzles and games. To check whether a number or string is palindrome or not, simply reverse it and compare it with the original number or string. If both are the same then the number or string is a palindrome, otherwise not.
These are different programming ways in Java to check Palindrome numbers and strings.
121, 1991, 1239321, MAAM, DAD, WOW, TENET, MADAM
When we reverse the above numbers or strings, they will remain the same. So, all the above are palindromes.
Java program to check palindrome using for loop
In the given code, we have used the for loop to check palindrome. Within this, we have reversed the sting and compared it with the original string. The for loop runs from the end to the beginning of the given string and accesses each character of the string using the charAt() method and stores it in y variable. The equalsIgnoreCase() method determines whether two strings are equal.
public class Palindrome
{
public static void main(String args[])
{
String x, y = "";
x = "MAAM";
int l = x.length();
for(int k = l - 1; k >= 0; k--)
{
y = y + x.charAt(k);
}
if(x.equalsIgnoreCase(y))
{
System.out.println("The string "+ x +" is palindrome.");
}
else
{
System. out.println("The string "+ x +" is not a palindrome.");
}
}
}
Output of the above code:
The string MAAM is palindrome.
Java program to check palindrome using while loop
In the given example, we have used the while loop to check the palindrome number. We store the number in a new variable, 'original'. Until the variable num is not equal to zero, find the reminder of the num and store it in a variable reverse. Next, divide the num by ten and repeat the previous process using a while loop, checking if the reverse number is equal to the original. If it is equal, print it is a palindrome.
// Java program to check palindrome
// number using while loop
public class Palindrome {
public static void main(String[] args) {
int num = 1295921, reverse = 0, remainder;
// store the number to original
int original = num;
// get the reverse of num
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
//Check Palindrome
if (original == reverse) {
System.out.println(original + " is Palindrome.");
}
else {
System.out.println(original + " is not Palindrome.");
}
}
}
Output of the above code:
1295921 is Palindrome.
Java program to check palindrome using recursion function
In the given example, we have checked whether the given string is a palindrome or not using a recursion function. A recursion function is a function which is called by itself. A recursion function continues until some condition is met to prevent it. That's why we use the if else statement to break the infinite recursion. Here, we have checked if the length is equal to zero or one, print it is a palindrome. In next condition, we have checked each character in substring from the front and rear, if it is found equal, print it is a palindrome.
public class CheckPalindrome{
public static boolean ChkPalindrome(String a){
if(a.length() == 0 || a.length() == 1){
return true;
}
if(a.charAt(0) == a.charAt(a.length()-1)){
return ChkPalindrome(a.substring(1, a.length()-1));
}
return false;
}
public static void main(String[]args){
String string = "WOW";
if(ChkPalindrome(string)){
System.out.println(string + " is a palindrome.");
}
else{
System.out.println(string + " is not a palindrome.");
}
}
}
Output of the above code:
WOW is a palindrome.
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number