Palindrome in Java Program for String and Integer (Number)

A Palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as madam or 1002001.

Some of Palindrome's Strings we use in our daily life are:
  • refer
  • dad
  • mom
  • noon
  • madam
  • level

Application of palindrome are in compression techniques, algorithms, security and much more.

Note: In Java or case sensitive programming languages palindrome program example [ Dad = not palindrome and and dad = palindrome ] are different.

Palindrome Java Program for Integer or Number

class PalindromeExample{  
 public static void main(String args[]){  
  int r,sum=0,temp;    
  int n=454;//It is the number variable to be checked for palindrome  
  
  temp=n;    
  while(n>0){    
   r=n%10;  //getting remainder  
   sum=(sum*10)+r;    
   n=n/10;    
  }    
  if(temp==sum)    
   System.out.println("palindrome number ");    
  else    
   System.out.println("not palindrome");    
}  
}  

Palindrome Java Program for String or Word or Number


import java.util.*;   
class PalindromeExample2  
{  
   public static void main(String args[])  
   {  
      String original, reverse = ""; // Objects of String class  
      Scanner in = new Scanner(System.in);   
      System.out.println("Enter a string/number to check if it is a palindrome");  
      original = in.nextLine();   
      int length = original.length();   
      for ( int i = length - 1; i >= 0; i-- )  
         reverse = reverse + original.charAt(i);  
      if (original.equals(reverse))  
         System.out.println("Entered string/number is a palindrome.");  
      else  
         System.out.println("Entered string/number isn't a palindrome.");   
   }  
}  


YOU MAY BE ALSO INTERESTED IN
Palindrome in Java Program for String and Integer (Number) Palindrome in Java Program for String and Integer (Number) Reviewed by Huzaif Sayyed on June 29, 2018 Rating: 5

No comments:

Powered by Blogger.