Search Here

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
    

    Palindrome Program in Java (Another way)

    You can also use a method where number or string is not predefined. Here, user has to put the number or string as input to check if the number/string is palindrome.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
    

    Palindrome Program in Java (Another way)

    You can also use a method where number or string is not predefined. Here, user has to put the number or string as input to check if the number/string is palindrome.

    1. import java.util.*;   
    2. class PalindromeExample2  
    3. {  
    4.    public static void main(String args[])  
    5.    {  
    6.       String original, reverse = ""// Objects of String class  
    7.       Scanner in = new Scanner(System.in);   
    8.       System.out.println("Enter a string/number to check if it is a palindrome");  
    9.       original = in.nextLine();   
    10.       int length = original.length();   
    11.       for ( int i = length - 1; i >= 0; i-- )  
    12.          reverse = reverse + original.charAt(i);  
    13.       if (original.equals(reverse))  
    14.          System.out.println("Entered string/number is a palindrome.");  
    15.       else  
    16.          System.out.println("Entered string/number isn't a palindrome.");   
    17.    }  
    18. }  

    Output:

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section