How to check whether a number is a Palindrome number or not in Java?

A Palindrome number is a special type of number. After reversing a number, if the number will be the same as the original number then that number will be called a Palindrome number.

For example, some Palindrome number is as follow:

121, 242, 12321, 515, 8448, 2345432, etc.

Let’s see the logic to check whether a given number is a Palindrome number or not,

Solution

CheckPalindromeNumber.java

package com.javacodepoint.programs;

import java.util.Scanner;

public class CheckPalindromeNumber {

	public static void main(String[] args) {
		
		// Taking input from the user to check Palindrome number

		System.out.print("Enter an integer number:");

		// Reading user input using Scanner object
		int number = new Scanner(System.in).nextInt();
		
		// Calling method to check Palindrome
		if(isPalindromeNumber(number)) {
			System.out.print("The given number is a Palindrome number.");
		}else {
			System.out.print("The given number is not a Palindrome number.");
		}

	}
	
	/*
	 * Method to check number is Palindrome number or not
	 */
	public static boolean isPalindromeNumber(int number) {
		int remainder, reverse=0;      
        
		//Storing number into a temporary variable  
        int temp = number;      
          
        //Reversing the number  
        while(number > 0){
        	//calculating remainder
        	remainder = number % 10;  
        	
        	//calculate reverse of the number when loop ends.
        	reverse = (reverse*10) + remainder;
        	
        	//removing last digit from the number
            number = number/10;      
        }   
  
        //Checking whether the reversing number is equal to the number or not  
        if(temp == reverse) {
        	return true;
        }
        return false;
	}

}

OUTPUT #1:

Enter an integer number:121
The given number is a Palindrome number.

OUTPUT #2:

Enter an integer number:123
The given number is not a Palindrome number.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework