Palindrome number and string

A palindrome is a number or a string that reads the same forward and backward. For example, the number 121 and the string “radar” are palindromes because they remain unchanged when reversed. Checking if a number or a string is a palindrome is a common task in programming, often used in problems related to string manipulation, number theory, and data validation.

This example demonstrates how to check if a given number or string is a palindrome using Java. The program includes methods to reverse a number and a string and then compare them to their original values.

Code Example

import java.util.Scanner;

public class PalindromeCheck {

    // Method to check if a given string is a palindrome
    public static boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;
        
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        
        return true;
    }

    // Method to check if a given number is a palindrome
    public static boolean isPalindrome(int number) {
        int originalNumber = number;
        int reversedNumber = 0;

        while (number != 0) {
            int digit = number % 10;
            reversedNumber = reversedNumber * 10 + digit;
            number /= 10;
        }

        return originalNumber == reversedNumber;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Check if a string is a palindrome
        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();
        if (isPalindrome(inputString)) {
            System.out.println("The string is a palindrome.");
        } else {
            System.out.println("The string is not a palindrome.");
        }

        // Check if a number is a palindrome
        System.out.print("Enter a number: ");
        int inputNumber = scanner.nextInt();
        if (isPalindrome(inputNumber)) {
            System.out.println("The number is a palindrome.");
        } else {
            System.out.println("The number is not a palindrome.");
        }
        
        scanner.close();
    }
}

Code Explanation

Class Definition

public class PalindromeCheck {

This line defines a new class named PalindromeCheck. This class contains methods to check if a string or a number is a palindrome and the main method to execute the program.

Method to Check if a String is a Palindrome

Method Signature
public static boolean isPalindrome(String str) {

This method isPalindrome takes a String parameter str and returns a boolean indicating whether the string is a palindrome.

Variables and Loop for Comparison
int left = 0;
int right = str.length() - 1;

while (left < right) {
    if (str.charAt(left) != str.charAt(right)) {
        return false;
    }
    left++;
    right--;
}
  • left and right are indices initialized to the start and end of the string, respectively.
  • A while loop compares characters from the start (left) and end (right) of the string.
  • If characters at left and right indices are not equal, the method returns false, indicating the string is not a palindrome.
  • The indices are incremented and decremented towards the center of the string.

Method to Check if a Number is a Palindrome

Method Signature
public static boolean isPalindrome(int number) {

This method isPalindrome takes an int parameter number and returns a boolean indicating whether the number is a palindrome.

Variables and Loop for Reversing the Number
int originalNumber = number;
int reversedNumber = 0;

while (number != 0) {
    int digit = number % 10;
    reversedNumber = reversedNumber * 10 + digit;
    number /= 10;
}
  • originalNumber stores the original number to compare later.
  • reversedNumber is used to build the reversed version of the number.
  • A while loop extracts digits from the end of number and appends them to reversedNumber.
Returning the Result
return originalNumber == reversedNumber;

The method returns true if the reversed number equals the original number, indicating it is a palindrome; otherwise, it returns false.

Main Method

Reading User Input
Scanner scanner = new Scanner(System.in);

This line creates a Scanner object to read input from the user.

Checking if a String is a Palindrome
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
if (isPalindrome(inputString)) {
    System.out.println("The string is a palindrome.");
} else {
    System.out.println("The string is not a palindrome.");
}

These lines prompt the user to enter a string, read the input, and check if it is a palindrome using the isPalindrome method for strings.

Checking if a Number is a Palindrome
System.out.print("Enter a number: ");
int inputNumber = scanner.nextInt();
if (isPalindrome(inputNumber)) {
    System.out.println("The number is a palindrome.");
} else {
    System.out.println("The number is not a palindrome.");
}

These lines prompt the user to enter a number, read the input, and check if it is a palindrome using the isPalindrome method for numbers.