Java Program to Check Palindrome Number and String
May 30, 2023
What is a Palindrome ?
A palindrome is a word, phrase, number, or sequence of characters that reads the same backward as forward. For example, the word “racecar” is a palindrome because it reads the same from left to right and from right to left. Similarly, the number 1221 is a palindrome because it remains the same when its digits are reversed. Palindromes are often used as exercises in wordplay, puzzles, or language learning, and they can also occur in literature, poetry, and music. Some famous examples of palindromes include “A man, a plan, a canal, Panama!” and “Madam, in Eden, I’m Adam.”
Steps to find a palindrome number :
Get the input number from the user.
Convert the number to a string or character array.
Traverse through half the length of the string or character array.
Compare the character at the current position with the character at the opposite end of the string or character array.
If the characters are not equal, the number is not a palindrome, and we can exit the loop.
If we reach the end of the loop without finding any unequal characters, the number is a palindrome.
Pseudo Code example for the above algorithm :
INPUT number
SET numberString = CONVERT number TO STRING
SET isPalindrome = true
FOR i = 0 TO (LENGTH numberString / 2) - 1
IF numberString[i] != numberString[LENGTH numberString - i - 1] THEN
SET isPalindrome = false
BREAK
END IF
END FOR
IF isPalindrome = true THEN
PRINT number + " is a palindrome number."
ELSE
PRINT number + " is not a palindrome number."
END IF
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = input.nextInt();
boolean isPalindrome = true;
String numberString = Integer.toString(number);
for (int i = 0; i < numberString.length() / 2; i++) {
if (numberString.charAt(i) != numberString.charAt(numberString.length() - i - 1)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println(number + " is a palindrome number.");
} else {
System.out.println(number + " is not a palindrome number.");
}
input.close();
}
}
Output :
Enter a number: 121
121 is a palindrome number.
Explanation :In this code, we use a for loop to traverse half the length of the string representation of the input number. We compare the characters at the current position and the opposite end of the string to check whether they are equal or not. If we find any unequal characters, we set the isPalindrome flag to false and exit the loop. Finally, we check the value of the isPalindrome flag and print the appropriate message.
Steps to find a palindrome String :
Get the input string from the user.
Convert the string to lowercase (optional).
Remove any non-alphanumeric characters from the string (optional).
Create a new string that is the reverse of the original string.
Compare the original string with its reverse.
If the two strings are equal, the original string is a palindrome.
Pseudo Code example for the above algorithm :
INPUT number
SET numberString = CONVERT number TO STRING
SET isPalindrome = true
FOR i = 0 TO (LENGTH numberString / 2) - 1
IF numberString[i] != numberString[LENGTH numberString - i - 1] THEN
SET isPalindrome = false
BREAK
END IF
END FOR
IF isPalindrome = true THEN
PRINT number + " is a palindrome number."
ELSE
PRINT number + " is not a palindrome number."
END IF
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String originalString = input.nextLine();
// Convert to lowercase and remove non-alphanumeric characters
String cleanedString = originalString.toLowerCase().replaceAll("[^a-z0-9]", "");
// Create a new string that is the reverse of the original string
StringBuilder reversedStringBuilder = new StringBuilder(cleanedString);
String reversedString = reversedStringBuilder.reverse().toString();
// Compare the original string with its reverse
if (cleanedString.equals(reversedString)) {
System.out.println(originalString + " is a palindrome string.");
} else {
System.out.println(originalString + " is not a palindrome string.");
}
input.close();
}
}
Output :
Enter a string: Radar
Radar is a palindrome string.
Explanation :In this code, we first convert the input string to lowercase and remove any non-alphanumeric characters using regular expressions. Then, we create a new string that is the reverse of the original string using a StringBuilder object. Finally, we compare the original string with its reverse and print the appropriate message.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment