Code with Vinay

ISC practical papers › ISC 2020 › Question 1

Prime Adam Numbers

ISC 2020 Computer Science Practical, Question 1 · Number programs

The question

A Prime-Adam integer is a positive integer (without leading zeros) which is prime as well as an Adam number.

Prime number: A number which has only two factors, i.e. 1 and the number itself. Example: 2, 3, 5, 7 ... etc.

Adam number: The square of a number and the square of it's reverse are reverse to each other. Example: If n=13 and reverse of 'n'=31, then, (13)^2 = 169 (31)^2 = 961 which is reverse of 169 thus 13, is an Adam number.

Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:

Test your program with following data and some random data:

Example 1
INPUT m = 5 n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100 n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS IS:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50 n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS IS:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700 n = 450
OUTPUT:
INVALID INPUT

Question: ISC 2020 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

ISC2020Q1.java Download GitHub
import java.util.*;

class ISC2020Q1 {
    // public static boolean isPrimeNumber(int n){
    // int count=0;
    // for( int divisor=1; divisor<=n; divisor++){
    // if(n%divisor==0) count++;
    // }
    // return count==2;
    // }
    public static boolean isPrimeNumber(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;
    }

    public static int getReverse(int n) {
        int reverse = 0;
        for (int temp = n; temp > 0; temp /= 10) {
            reverse = reverse * 10 + temp % 10;
        }
        return reverse;
    }

    public static boolean isAdamNumber(int n) {
        int reverse = getReverse(n);
        return n * n == getReverse(reverse * reverse);
    }

    public static boolean isPrimeAdamNumber(int n) {
        return isAdamNumber(n) && isPrimeNumber(n);
    }

    public static void main(String[] args) {
        int m, n;
        Scanner input = new Scanner(System.in);
        System.out.println("INPUT:");
        System.out.print("m = ");
        m = input.nextInt();

        System.out.print("n = ");
        n = input.nextInt();

        System.out.println("OUTPUT:");
        if (m <= n) {
            int frequency = 0;
            System.out.println("THE PRIME-ADAM INTEGERS ARE:");
            for (int i = m; i <= n; i++) {
                if (isPrimeAdamNumber(i)) {
                    frequency++;
                    System.out.print(i + " ");
                }
            }
            if (frequency == 0) {
                System.out.println("\bNIL");
            } else {
                System.out.println("\b\nFREQUENCY OF PRIME-ADAM INTEGERS IS: " + frequency);
            }
        } else {
            System.out.println("INVALID INPUT");
        }
        input.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