Prime Palindrome ISC Computer Science 2012 Practical
A prime palindrome integer is a positive integer (without leading zeros) which is prime as well as a palindrome.
Given two 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 NUMBERS ARE:
101, 131, 151, 181, 191, 313, 353, 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
class PrimePalindrome{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("n = ");
int n = Integer.parseInt(in.nextLine());
if(m > n || m > 2999 || n > 2999){
System.out.println("OUT OF RANGE");
return;
}
int f = 0;
System.out.println("THE PRIME PALINDROME NUMBERS ARE:");
for(int i = m; i <= n; i++){
if(isPrime(i) && isPalindrome(i)){
if(f == 0)
System.out.print(i);
else
System.out.print(", " + i);
f++;
}
}
if(f == 0)
System.out.println("NIL");
else
System.out.println();
System.out.println("FREQUENCY OF PRIME PALINDROME INTEGERS: " + f);
}
public static boolean isPrime(int n){
for(int i = 2; i <= n / 2; i++){
if(n % i == 0)
return false;
}
return true;
}
public static boolean isPalindrome(int n){
int rev = 0;
for(int i = n; i != 0; i /= 10)
rev = rev * 10 + i % 10;
return n == rev;
}
}
Comments
Post a Comment