Palindrome Means is a word ,number,etc.which reads the same backward and forward.
Ex: "151" is palindrome.after reversing number we are getting same number.so It's a palindrome number.
"152" is not palindrome.
import java.util.Scanner;
public class palindrome {
public static void main(String[] args) {
int n,temp,rev=0,r;
System.out.println("Enter any number:");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
temp=n;
while(temp>0)
{
r=temp%10;
rev =(rev*10)+r;
temp=temp/10;
}
if(n==rev)
{
System.out.println("Is a palindrome number");
}
else
{
System.out.println("Not a palindrome number");
}
}
}
Output:
Enter any number:
151
Is a palindrome number
We have used Scanner class to take the input from the user.In while loop "temp%10" is there when we divide the number using modulous we get remainder as ouput.
No comments:
Post a Comment