Palindrome

A palindrome is anything that reads the same backward and forward. In other words after reversing any number, word or phrase if it remains the same as original then it is a palindrome.

Logic

To check whether a number is a palindrome number or not we have to reverse it first. After reversing it we compare the reversed number with the original number. If both the numbers are similar then it means the number we have is a palindrome number.

Program

import java.util.Scanner;

public class CheckPalindrome {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int num, temp, reverse = 0;

        // Take input from user
        System.out.print("Enter any number : ");
        num = scanner.nextInt();

        temp = num;

        while(temp != 0) {
        
            reverse = (reverse * 10) + (temp % 10);
            
            temp /= 10;
        }

        // Check for palindrome
        if(reverse == num) {
            System.out.println("\n" + num + " is a palindrome.");
        } else {
            System.out.println("\n" + num + " is not a palindrome.");
        }
    }

}

Output

Enter any number : 121
121 is a palindrome.