# Java program to print all prime palindrome number between m and n

Write a program in java to print all prime Palindrome numbers between 'm' and 'n'

Sample Example:

Enter the value of m :

1

Enter the value of n :

1000

Prime Palindrome number between 1 and 1000 are:

2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929

Solution 1:

``````import java.util.*;
class PrimePalindrome
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
int m,n,i,a,digit,rev;
boolean flag;
System.out.println("Enter the value of m :");
m=sc.nextInt();
System.out.println("Enter the value of n :");
n=sc.nextInt();
System.out.println("Prime Palindrome number between "+m+" and "+n+" are: ");
for(i=m;i<=n;i++)
{
a=i;
rev=0;
while(a>0)
{
digit=a%10;
rev=rev*10+digit;
a=a/10;
}
if(i==rev)
{
flag=true;
for(a=2;a<i;a++)
{
if(i%a==0)
{
flag=false;
break;
}
}
if(flag==true&&i>1)
{
System.out.print(i+" ");
}
}
}
}
}``````

Solution 2: Using Function

``````import java.util.*;
class PrimePalindrome
{
boolean isPrime(int n)
{
int a;
for(a=2;a<=n/2;a++)
{
if(n%a==0)
return false;
}
if(n==1)
return false;
else
return true;
}
boolean isPalindrome(int n)
{
int rev=0,t;
t=n;
while(t>0)
{
rev=rev*10+(t%10);
t=t/10;
}
if(rev==n)
return true;
else
return false;
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
int m,n,i,a,digit,rev;
PrimePalindrome obj=new PrimePalindrome();
boolean flag;
System.out.println("Enter the value of m :");
m=sc.nextInt();
System.out.println("Enter the value of n :");
n=sc.nextInt();
System.out.println("Prime Palindrome number between "+m+" and "+n+" are: ");
for(i=m;i<=n;i++)
{
if(obj.isPalindrome(i)&&obj.isPrime(i))
{
System.out.print(i+" ");
}

}
}
}``````
`Output`

Enter the value of m :

1

Enter the value of n :

1000

Prime Palindrome number between 1 and 1000 are:

2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929