Java Program to Check Palindrome Number and String

Java Program to Check Palindrome Number and String

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 :

  1. Get the input number from the user.
  2. Convert the number to a string or character array.
  3. Traverse through half the length of the string or character array.
  4. Compare the character at the current position with the character at the opposite end of the string or character array.
  5. If the characters are not equal, the number is not a palindrome, and we can exit the loop.
  6. 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

Example 1 : To check Palindrome Number.

Run

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.

Steps to find a palindrome String :

  1. Get the input string from the user.
  2. Convert the string to lowercase (optional).
  3. Remove any non-alphanumeric characters from the string (optional).
  4. Create a new string that is the reverse of the original string.
  5. Compare the original string with its reverse.
  6. 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

Example 2 : To Check Palindrome String .

Run

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.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription