Write a java program to check if a number is a palindrome in java? (121 is a pelindrome, 321 is not)
Answers
class Main{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
Explanation:
import java.util.*;
class palindrome
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter any number");
int n=sc.nextInt();
int d=0;
int r=1;
int temp;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
if(r==temp)
System.out.print("Palindrome number");
else
System.out.print("Not a palindrome number");
}
}