Home >>Java Programs >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.
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");
}
}
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.");
}
}