Code with Vinay

ISC practical papers › ISC 2012 › Question 1

Prime Palindrome Integer

ISC 2012 Computer Science Practical, Question 1 · Number programs

The question

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, 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

Question: ISC 2012 Computer Science Paper 2 (Practical), Question 1, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2012Q1.java Download GitHub
import java.util.*;
class PrimePalindrome{
    private int m,n,frequency;
    private String output;
    public PrimePalindrome(int m , int n){
        this.m=m;
        this.n=n;
        process();
    }
    private static boolean isPrime( int n){
        if(n<2) return false;
        else if(n==2) return true;
        else if(n%2==0) return false;
        else{
            int limit = (int)Math.sqrt(n);
            for(int divisor=3; divisor<=limit; divisor+=2){
                if(n%divisor==0)return false;
            }
        }
        return true;
    }
    private static boolean isPalindrome( int n){
        int reverse=0;
        for( int temp=n;temp>0;temp/=10){
            reverse=reverse*10+(temp%10);
        }
        return n==reverse;
    }
    public void process(){
        output="";
        frequency=0;
        for( int number = m%2==0?m+1:m; number<=n; number+=2){
            if(isPalindrome(number) && isPrime(number)){
                frequency++;
                output+=number+", ";
            }
        }
        output=output.substring(0,output.length()-2);
    }
    public String toString(){
        return output;
    }
    public int getFrequency(){
        return frequency;
    }
}
class ISC2012Q1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tm = ");
        int m = in.nextInt();
        System.out.print("\tn = ");
        int n = in.nextInt();
        if( m < 3000 && n<3000){
            PrimePalindrome obj= new PrimePalindrome(m, n);
            System.out.print("OUTPUT:\tTHE PRIME PALINDROME INTEGERS ARE:\n\t");
            System.out.println(obj);
            System.out.println("\tFREQUENCY OF PRIME PALINDROME INTEGERS: "+obj.getFrequency());
        }else{
            System.out.println("OUTPUT:\tOUT OF RANGE");
        }
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers