Write a java program to find the given number is palindrome or not.


 

Palindrome Program in Java

Palindrome number in java: A palindrome number is a number that is same after reverse. For example 545, 151, 34543, 343, 171, 48984 are the palindrome numbers. It can also be a string like LOL, MADAM etc.

Palindrome number algorithm

  • Get the number to check for palindrome
  • Hold the number in temporary variable
  • Reverse the number
  • Compare the temporary number with reversed number
  • If both numbers are same, print "palindrome number"
  • Else print "not palindrome number"

Let's see the palindrome program in java. In this java program, we will get a number variable and check whether number is palindrome or not.

  1. class PalindromeExample{  
  2.  public static void main(String args[]){  
  3.   int r,sum=0,temp;    
  4.   int n=454;//It is the number variable to be checked for palindrome  
  5.   
  6.   temp=n;    
  7.   while(n>0){    
  8.    r=n%10;  //getting remainder  
  9.    sum=(sum*10)+r;    
  10.    n=n/10;    
  11.   }    
  12.   if(temp==sum)    
  13.    System.out.println("palindrome number ");    
  14.   else    
  15.    System.out.println("not palindrome");    
  16. }  
  17. }  

Output:

palindrome  number