Write a program to accept the lower range and upper range
from the user and display the Prime Palindrome number in that range.
Methods used boolean isPrime()
boolean isPalindrome()
void generate() and main()
import java.io.*;
class PalPrime
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
i,lower,upper;
public void
generate() throws Exception
{
System.out.println("Enter the lower range:");
lower=Integer.parseInt(br.readLine());
System.out.println("Enter the upper range:");
upper=Integer.parseInt(br.readLine());
System.out.print(" The Prime Palindrome numbers
are:");
for(i=lower;i<=upper;i++)
{
if(isPrime(i)
&& isPalindrome(i))
System.out.print(i+ " ");
}
}
private
boolean isPrime(int n)
{
for(i=2;i< n;i++)
{
if(n%i==0)
break;
}
if(i==n)
return true;
else
return false;
}
private boolean
isPalindrome(int n)
{
int rev=0;
for(i=n;i>0;i=i/10)
{
rev=rev*10+i%10;
}
if(rev==n)
return true;
else
return false;
}
public
static void main(String args[])throws Exception
{
PalPrime
ob=new PalPrime();
ob.generate();
}
}
No comments:
Post a Comment