🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

WAP to check whether a number is Palindrome in Java  


What is Palindrome Number ?
  • A palindrome number is a number that reads the same forward and backward.
  • For example, 121, 545 and 1331 are palindrome numbers.
Logical Steps:
  • Store the original number in a temporary variable.
  • Reverse the digits of the number using a loop.
  • Compare the reversed number with the original number.
  • If they are the same, it’s a palindrome; otherwise, it’s not.
Programs:
  • Below is the simple program:
    public class PalindromeNumber
    {
        public static void main(String[] args)
        {
            int no = 52325;  // Number to check for palindrome
            int originalNumber = no;
            int reversedNumber = 0;
    
            while (no != 0)
            {
                int digit = no % 10;  // Get the last digit
                reversedNumber = reversedNumber * 10 + digit;  // Build the reversed number
                no = no / 10;  // Remove the last digit
            }
    
            // Check if the original number and reversed number are the same
            if (originalNumber == reversedNumber)
            {
                System.out.println(originalNumber + " is a Palindrome number.");
            }
            else
            {
                System.out.println(originalNumber + " is not a Palindrome number.");
            }
        }
    }
    Output:
    52325 is a Palindrome number.
  • Below is the program by taking user input:
    import java.util.Scanner;
    
    public class PalindromeNumber
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
    
            System.out.print("Enter the number: ");
            int no = scanner.nextInt();  // Number to check for palindrome
            int originalNumber = no;
            int reversedNumber = 0;
    
            while (no != 0)
            {
                int digit = no % 10;  // Get the last digit
                reversedNumber = reversedNumber * 10 + digit;  // Build the reversed number
                no = no / 10;  // Remove the last digit
            }
    
            // Check if the original number and reversed number are the same
            if (originalNumber == reversedNumber)
            {
                System.out.println(originalNumber + " is a Palindrome number.");
            }
            else
            {
                System.out.println(originalNumber + " is not a Palindrome number.");
            }
        }
    }
    Output:
    Enter the number: 121
    121 is a Palindrome number.
Program Explanation:
  • Initialization and Variables:
    • A variable no is initialized with the number to check (e.g., 121).
    • Two more variables:
      • originalNumber stores the original number for later comparison.
      • reversedNumber is initialized to 0 to build the reversed number.
  • Reversing the Number:
    • A while loop runs until no becomes 0:
      • Extract the last digit using no % 10.
      • Add this digit to reversedNumber after shifting it by one place (multiply by 10).
      • Remove the last digit from no using no / 10.
  • Comparison:
    • After the loop, compare originalNumber with reversedNumber
      • If they are the same, the number is a Palindrome.
      • Otherwise, it's not a Palindrome.
  • Output:
    • Print whether the original number is a palindrome or not.