A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome. Given two positive integers m and n, where m < n, write a program to determine how many prime-palindrome integers are there in the range between m and n (both inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the number of prime palindrome integers in the specified range along with their values in the format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT: M = 100
N = 1000
OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15
Example 2
INPUT: M = 100
N = 5000
OUTPUT: Out of range
import java.util.*;
class ISC2012q01
{
public static void main (String args[])
throws InputMismatchException
{
Scanner scan = new Scanner(System.in);
int m,n,i,j,t,c,r,a,freq;
System.out.println("Enter two positive integers m and n,
where m < 3000 and n < 3000: ");
m = scan.nextInt();
n = scan.nextInt();
if(m < 3000 && n < 3000){
// To count the frequency of prime-palindrome numbers
freq = 0;
System.out.println("The prime palindrome integers are:");
for(i=m;i<=n;i++){
t = i;
//Check for prime
c = 0;
for(j =1; j<=t; j++){
if(t%j == 0)
c++;
}
if(c == 2){
//Check for palindrome
r = 0;
while(t>0){
a = t%10;
r = r*10 + a;
t = t/10;
}
if(r == i){
System.out.print(i+" ");
freq++;
}
}
}
System.out.println("\nFrequency of prime palindrome
integers:"+freq);
}else{
System.out.println("OUT OF RANGE");
}
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments