Palindrome Program in Java: Check number is Palindrome or Not

What is Palindrome Number?

A Palindrome Number is a number that even when reversed is same as original number

Examples of Palindrome Number

121, 393, 34043, 111, 555, 48084 

Examples of Palindrome Number

 LOL, MADAM
Program Logic

How to check whether input number is Palindrome or not

package com.gtupapers;
 
public class PalindromeNum {
 
	public static void main(String[] args)
	{
 
		int lastDigit,sum=0,a;    
		int inputNumber=171; //It is the number  to be checked for palindrome 
 
		a=inputNumber; 
        
        // Code to reverse a number
		while(a>0)
		{   System.out.println("Input Number "+a);  
			lastDigit=a%10; //getting remainder  
			System.out.println("Last Digit "+lastDigit); 
			System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); 
			sum=(sum*10)+lastDigit;  
			a=a/10;
			
		}    
 
		// if given number equal to sum than number is palindrome otherwise not palindrome
		if(sum==inputNumber)    
			System.out.println("Number is palindrome ");    
		else    
			System.out.println("Number is not palindrome");    
	}
}

Code Output:

Input Number 171
Last Digit 1
Digit 1 was added to sum 0
Input Number 17
Last Digit 7
Digit 7 was added to sum 10
Input Number 1
Last Digit 1
Digit 1 was added to sum 170
Number is palindrome

 

YOU MIGHT LIKE: