Home >>Java Programs >Check Palindrome Number in Java

Check Palindrome Number in Java

Check Palindrome Number in Java

In this example, we will see a Java program through which we will check if the given input number or string is a Palindrome or not.

A given number is said to be a palindrome number if that number is the same after reverse. For example 545, 151, 34543 are the palindrome numbers.

Algorithm:

  • STEP 1 : Get the number to check for the palindrome.
  • STEP 2 : Hold the number in a temporary variable.
  • STEP 3 : Reverse that number.
  • STEP 4 : Compare the temporary number with the reversed number.
  • STEP 5 : If both numbers are the same, print "palindrome number".
  • STEP 6 : Else print "not palindrome number".
Example 1

class Main
{  
 public static void main(String args[])
 {  
  int r,sum=0,temp;    
  int n=872232278;//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("Given nuumber is Palindrome ");    
  else    
  System.out.println("not palindrome");    
}  
}  

Output:
Given nuumber is Palindrome

Palindrome Program in Java(Different way)

Example 2

import java.util.*;   
class Main
{  
   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.");   
   }  
}  

Output:
Enter a string/number to check if it is a palindrome
ABCDEDCBA
Entered string/number is a palindrome.

Java Programs Check Palindrome Number in Java Factorial Program using loop in java Factorial Program using recursion in java Fibonacci Series Program in Java using recursion Fibonacci series without using recursion in Java Find an Armstrong Number in Java Prime Number Program in Java Find Prime numbers between two numbers in Java Break statement in Java for each loop in Java Typecasting in Java Printing the format text with printf in Java How to generate random numbers within a range in Java Java Program to count all punctuation characters in the string Java program to print the following given pattern Java program to print the following given pattern Java program to print the following given pattern Java program to print the following pattern Java program to print the following pattern Java program to print the given pattern Java program to print the given pattern Java program to print the given pattern Java program to print the following pattern Java program to print the following pattern Java Program to Print the following Pattern Java program to print the following pattern Java Program to Print the following Pattern Java Program to print the smallest element in an array Java Program to Print the following pattern Java Program to Print the following pattern Java Program to Print the following Pattern Java Program to Copy One Array to Another in Java Java Program to find the frequency of each element in a array Java Program to left rotate the elements of an array Java Program to print the duplicate elements of an array Java Program to print the elements of an array Java Program for binary search Java Program for linear search Java Program for bubble sort Java Program for insertion sort Java Program for selection sort Java Program to print the elements of an array present on even position Java Program to print the elements of an array in reverse order Java Program to find Third Largest Number in an Array Java Program to print the largest element in an array Print the number of elements in an array java Java Program to print the sum of all the items of the array Java Program to right rotate the elements of an array Java Program to sort the elements of an array in ascending order Java Program to sort the elements of an array in descending order Java Program to print the elements of an array present on odd position Java Program to Check if it is a Sparse Matrix Java Program to check a given matrix is an identity matrix Java Program to determine whether two matrices are equal Java Program to display the lower triangular matrix Java Program to find the product of two matrices
No Sidebar ads