Java Program to Check Whether a Number is Palindrome or Not

  • How to check whether a number is palindrome or not using loop.

An integer is called palindrome number, If the number remains same after reversing the sequence of it's digits. To check whether a number is palindrome number or not, we will create a copy of input number and reverse it's digits. If reversed number is same as input number then it is a palindrome number other not a palindrome number.

Algorithm to check whether a number is palindrome or not
  1. Take a number as input from user and store it in an integer variable(Let's call it num).
  2. Reverse the digits of num, and store it in another integer variable(Let's call it reverse).
  3. Compare num and reverse.
  4. If both are equal then num is palindrome number otherwise not a palindrome number.

Java program to check for palindrome numbers

package com.tcc.java.programs;

import java.util.*;

public class PalindromeNumberCheck {
    public static void main(String args[]) {
        int num, temp, reverse = 0, rightDigit;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a number");
        num = in.nextInt();
        temp = num;
        
        /*reverse num and store it in reverse */
        while(temp != 0){
            rightDigit = temp % 10;
            reverse = reverse*10 + rightDigit;
            temp = temp/10;
        }
     
        if(reverse == num)
            System.out.print(num + " is Palindrome number");
        } else {
            System.out.print(num + " is not a Palindrome number");
        }
    }
}
Output
Enter a number
1234321
1234321 is Palindrome number
Enter a number
72537625
72537625 is not a Palindrome number

Recommended Posts
Java Program to Check Whether a Number is Armstrong Number or Not
Java Program to Reverse a Number using Recursion
Java Program to Check Perfect Number or Not
Java Program to Check a Number is Prime Number or Not
Java Program to Check If a Year is Leap Year or Not
Java Program to Count Number of Digits in a Number
Java Program to Find Factorial of a Number using For Loop
Java Program to Find LCM and GCD of Two Numbers
Java Program to Check Two Strings are Anagram or Not
Java Program to Check Whether two Strings are Equal or Not
C++ Program to check Whether a Number is Palindrome or Not
All Java Programs