Question
Print Prime Palindrome number in given range
A prime palindrome integer is a positive integer (without leading zeroes) which is prime as well as a palindrome.
ENTER THE LOWER RANGE
1
ENTER THE UPPER RANGE
200
2 3 5 7 11 101 131 151 181 191
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class PrimePalindromeNumber
{
public static void main(String args[])
{
long lowerRange=0,upperRange=0,temp=0,i=0,reverse=0,j=0;
int flag=0;
boolean isPrime=false,isPalindrome=false;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER RANGE");
lowerRange=sc.nextLong();
System.out.println("ENTER THE UPPER RANGE");
upperRange=sc.nextLong();
for(i=lowerRange;i<=upperRange;i++)
{
temp=i;
flag=0;
isPrime=false;
isPalindrome=false;
if(temp< 2)
{
isPrime=false;
}
else
{
/*for loop runs till "temp/2" because number is divisible perfected till half the number*/
for(j=2;j<=temp/2;j++)
{
/*Checking if number is divisible by j.*/
if(temp%j==0)
{
isPrime=false;
flag=1;
break;
}
}
if(flag==0)
{
isPrime=true;
}
}
temp=i;
reverse=0;
while(temp>0)
{
reverse=reverse*10+temp%10;
temp=temp/10;
}
if(reverse==i)
{
isPalindrome=true;
}
else
{
isPalindrome=false;
}
if(isPrime==true && isPalindrome==true)
{
System.out.print(i+" ");
}
}
}
}