write a program to accept a no. and find that no. is a palindrom or not using function.
static class Palindrome
{
int n;
Palindrome()
{
n=121;
}
Palindrome(int x)
{
n=x;
}
void checkPallin()
{
int a;
a=n;
int s=0,r;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(a==s)
System.out.println("Given Number is Palindrome");
else
System.out.println("Given Number is not Palindrome")
}
}
public static void main(String[] args)
{ int x = Integer.parseInt(s[0]); Palindrome p1=new Palindrome();
p1.checkPallin();
Palindrome p2=new Palindrome(x);
p2.checkPallin();
}
}
Hi Friend,
You can try the following code:
import java.util.*; public class CheckPalindomeNumber { public static void main(String [] args){ Scanner input=new Scanner(System.in); System.out.println("Enter number"); int num= input.nextInt(); int n = num; int rev=0; for (int i=0; i<=num; i++){ int r=num%10; num=num/10; rev=rev*10+r; i=0; } if(n == rev){ System.out.print("Number is palindrome!"); } else{ System.out.println("Number is not palindrome!"); } } }
Thanks
Ads