C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2020 Practical Paper ISC Computer Science



Share with a Friend

Solved 2020 Practical Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Practical Papers

Prime Adam Number Program - ISC 2020 Practical

A Prime-Adam integer is a positive integer (without leading zeroes) which is a 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 its reverse are reverse to each other. Example: If n = 13 and reverse of ‘n’ is 31, then, 132 = 169, and 312 = 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 the 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 are:
101, 103, 113
Frequency of Prime-Adam integers is: 3

Example 3:
INPUT:
m = 50
n = 70
OUTPUT:
The Prime-Adam integers are:
NIL
Frequency of Prime-Adam integers is: 0

Example 4:
INPUT:
m = 700
n = 450
OUTPUT:
Invalid Input.

import java.io.*; class PrimeAdam{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("m = "); int m = Integer.parseInt(br.readLine()); System.out.print("n = "); int n = Integer.parseInt(br.readLine()); if(m >= n){ System.out.println("Invalid input."); return; } int count = 0; System.out.println("The Prime-Adam integers are:"); for(int i = m; i <= n; i++){ if(isPrime(i)){ int rev = reverse(i); int s1 = i * i; int s2 = rev * rev; if(reverse(s1) == s2){ if(count == 0) System.out.print(i); else System.out.print(", " + i); count++; } } } if(count == 0) System.out.println("NIL"); else System.out.println(); System.out.println("Frequency of Prime-Adam integers is: " + count); } public static boolean isPrime(int n){ int f = 0; for(int i = 1; i <= n; i++){ if(n % i == 0) f++; } return f == 2; } public static int reverse(int n){ int rev = 0; for(int i = n; i > 0; i /= 10) rev = rev * 10 + i % 10; return rev; } }

Output

OUTPUT 1:
m = 5
n = 100
The Prime-Adam integers are:
11, 13, 31
Frequency of Prime-Adam integers is: 3

OUTPUT 2:

m = 100
n = 200
The Prime-Adam integers are:
101, 103, 113
Frequency of Prime-Adam integers is: 3

OUTPUT 3:

m = 50
n = 70
The Prime-Adam integers are:
NIL
Frequency of Prime-Adam integers is: 0