[Question 1] ISC 2020 Computer Practical Paper Solved – Prime Adam Number
Prime Adam Number ISC 2020 practical solved with algorithm, explanation, Java program and equivalent Python code.
Click here to download the complete ISC 2020 Computer Science Paper 2 (Practical).
Question:
A Prime-Adam integer is a positive integer (without leading zeros) 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’ =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 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.
Algorithm:
Step 1: Start.
Step 2: Input the lower limit m and upper limit n.
Step 3: If either limit is less than 1, or if m is greater than n, display INVALID INPUT and stop.
Step 4: For every number from m to n, test whether it is prime by counting its factors.
Step 5: Reverse the number, square the original number, square the reversed number, and reverse the square of the original number.
Step 6: If the number is prime and the reversed square is equal to the square of the reversed number, print the number and increase the frequency counter.
Step 7: If no such number is found, print NIL.
Step 8: Display the frequency of Prime-Adam integers.
Step 9: Stop.
Explanation:
A Prime-Adam number must satisfy two conditions at the same time. First, it must be a prime number, so it should have exactly two factors: 1 and the number itself. Secondly, it must be an Adam number. To check the Adam property, the program reverses the number, squares both the original number and its reverse, and then compares the reverse of the original square with the square of the reversed number. For example, 13 becomes 31 when reversed. The square of 13 is 169 and the square of 31 is 961. Since 961 is the reverse of 169, 13 is an Adam number. Since 13 is also prime, it is a Prime-Adam number.
The program first validates the range because the question allows only positive integers and requires the lower limit to be less than or equal to the upper limit. After that, each integer in the range is checked one by one. A helper function checks primality, another helper function reverses digits, and a third helper function checks the Adam number condition. This keeps the logic clean and makes the main method easy to follow. A counter stores how many Prime-Adam integers are printed. If the counter remains zero, the output shows NIL; otherwise, the valid numbers and their frequency are displayed.
For a dry run, consider the range 100 to 200. The program checks 100 first, rejects it because it is not prime, and continues. When it reaches 101, the number is prime. Its reverse is also 101, so the square comparison succeeds and 101 is printed. For 103, the square is 10609 and the reverse number 301 gives 90601; the reverse of 10609 is also 90601, so 103 qualifies. This trace shows why both tests must be performed together. A number that is only prime or only Adam must not be printed.
Programming Code:
/**
* The class ISC2020_Q1 inputs a lower and an upper range
* and prints all the Prime-Adam numbers within that range
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2020 Question 1
*/
import java.util.*;
class ISC2020_Q1 //the main class
{
boolean isPrime(int n) //to check for prime number
{
int c = 0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
{
c++;
}
}
if(c == 2)
return true;
else
return false;
}
int reverseNum(int n) //to reverse a number
{
int r = 0, d = 0;
while(n > 0)
{
d = n%10;
r = r*10 + d;
n = n/10;
}
return r;
}
boolean isAdam(int n) //to check for Adam number
{
int rev = reverseNum(n);
int sqn = n*n;
int sqr = rev * rev; //square of reverse
int rsqn = reverseNum(sqn); //reverse of square
if(rsqn == sqr)
return true;
else
return false;
}
public static void main(String args[]) //the main method
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the lower limit : ");
int m = sc.nextInt();
System.out.print("Enter the upper limit : ");
int n = sc.nextInt();
ISC2020_Q1 ob = new ISC2020_Q1();
if(m<1 || n<1 || m>n) //checking for invalid input
{
System.out.println("INVALID INPUT");
}
else
{
int c = 0;
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for(int i=m; i<=n; i++)
{
if(ob.isPrime(i) && ob.isAdam(i)) //checking for prime-adam number
{
c++;
System.out.print(i + "\t");
}
}
if(c == 0)
System.out.print("NIL");
System.out.println("\nFREQUENCY OF PRIME-ADAM INTEGERS IS:" + c);
}
}
}Equivalent Python Program:
def reverse_num(n):
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10
return rev
def is_prime(n):
if n < 2:
return False
count = 0
for i in range(1, n + 1):
if n % i == 0:
count += 1
return count == 2
def is_adam(n):
rev = reverse_num(n)
square = n * n
reverse_square = reverse_num(square)
reverse_number_square = rev * rev
return reverse_square == reverse_number_square
m = int(input("Enter the lower limit : "))
n = int(input("Enter the upper limit : "))
if m < 1 or n < 1 or m > n:
print("INVALID INPUT")
else:
count = 0
result = []
print("THE PRIME-ADAM INTEGERS ARE:")
for num in range(m, n + 1):
if is_prime(num) and is_adam(num):
result.append(num)
count += 1
if count == 0:
print("NIL")
else:
print(*result)
print("FREQUENCY OF PRIME-ADAM INTEGERS IS:", count)Output:
Enter the lower limit : 100
Enter the upper limit : 200
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS:3
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.