ISC Practicals and Theory - Prime Adam Number (OOP)
Updated 20 Oct 2026
This version writes the Prime-Adam number program as a class whose constructor stores the range $m$ to $n$ (with $m \ge 1$ and $m < n$), with a separate method for every job. A Prime-Adam number is a number that is both prime and an Adam number.
Definition
A prime number has exactly two factors, 1 and itself. An Adam number is a number whose square and the square of its reverse are reverses of each other. A Prime-Adam number is both. For example $13$ is prime, $13^2 = 169$, $31^2 = 961$, and $169$ reversed is $961$, so 13 is a Prime-Adam number.
Single-digit numbers: $2^2 = 4$ and $3^2 = 9$ are their own reverses, so 2 and 3 are Prime-Adam numbers. The number 1 is not prime, so it is not a Prime-Adam number.
Algorithm
mainreadsaandb, then creates the object withnew PrimeAdam(a, b). The constructor copies them into the data membersmandnand setsfreqto 0.isValid()returns true only whenmis at least 1 and less thann. If it returns false,mainprintsINVALID INPUT.- Otherwise
maincallsshowNumbers(), which loopsifrommtonand for eachichecksisPrime(i)andisAdam(i). Every number that passes is printed andfreqis increased by 1.NILis printed if none is found. mainthen callsshowFrequency()to printfreq.isPrime(x)counts the factors ofxfrom 1 tox; it is prime if the count is 2.reverse(x)repeatedly appends the last digit (rev = rev * 10 + x % 10) and removes it (x = x / 10) untilxis 0.isAdam(x)findssq,randrsq(the squares and the reverse) and returns true ifreverse(sq)equalsrsq.
Program
The constructor sets the range, isValid checks it, isPrime, reverse and isAdam do the tests, and showNumbers and showFrequency print the result.
import java.util.Scanner;
/* Class PrimeAdam stores a range m to n and finds the Prime-Adam numbers in it.
A Prime-Adam number is a number that is both prime and an Adam number. */
public class PrimeAdam
{
int m; // lower limit of the range
int n; // upper limit of the range
int freq; // count of Prime-Adam numbers found in the range
/* Constructor: sets the limits of the range and the frequency to zero.
Parameters: a, b - the lower and upper limit. */
PrimeAdam(int a, int b)
{
m = a;
n = b;
freq = 0;
}
/* Checks whether the range is acceptable (m positive and less than n).
No parameters. Returns true if the range is valid, else false. */
boolean isValid()
{
return m >= 1 && m < n;
}
/* Checks whether a number is prime.
Parameter: x - the number to test. Returns true if x is prime, else false. */
boolean isPrime(int x)
{
int count = 0; // number of factors of x
for (int i = 1; i <= x; i++)
{
if (x % i == 0) // i divides x, so i is a factor
count++;
}
return count == 2; // a prime has exactly two factors
}
/* Finds the reverse of a number.
Parameter: x - the number to reverse. Returns the reversed number. */
int reverse(int x)
{
int rev = 0; // reversed number built so far
while (x > 0)
{
rev = rev * 10 + x % 10; // append last digit of x to rev
x = x / 10; // drop the last digit of x
}
return rev;
}
/* Checks whether a number is an Adam number.
Parameter: x - the number to test. Returns true if square of x and
square of its reverse are reverses of each other, else false. */
boolean isAdam(int x)
{
int sq = x * x; // square of the number
int r = reverse(x); // reverse of the number
int rsq = r * r; // square of the reverse
return reverse(sq) == rsq; // reverse of sq must equal rsq
}
/* Displays every Prime-Adam number from m to n and updates freq.
No parameters. Returns nothing. */
void showNumbers()
{
System.out.println("The Prime-Adam integers are:");
for (int i = m; i <= n; i++)
{
if (isPrime(i) && isAdam(i)) // both conditions must hold
{
System.out.print(i + " ");
freq++;
}
}
if (freq == 0)
System.out.print("NIL");
System.out.println();
}
/* Displays the frequency of Prime-Adam numbers found.
No parameters. Returns nothing. */
void showFrequency()
{
System.out.println("Frequency of Prime-Adam integers is: " + freq);
}
/* Reads m and n, creates the object and calls its methods. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter the lower limit m: ");
int a = sc.nextInt(); // value of m typed by the user
System.out.print("Enter the upper limit n: ");
int b = sc.nextInt(); // value of n typed by the user
PrimeAdam ob = new PrimeAdam(a, b); // constructor stores the range
if (ob.isValid())
{
ob.showNumbers();
ob.showFrequency();
}
else
System.out.println("INVALID INPUT");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| m | int | Data member: lower limit of the range |
| n | int | Data member: upper limit of the range |
| freq | int | Data member: count of Prime-Adam numbers found |
| a, b (in constructor) | int | Parameters of the constructor: the lower and upper limit |
| x (in isPrime) | int | Parameter: the number being tested for prime |
| count | int | Number of factors of x found so far |
| i (in isPrime) | int | Loop counter that runs through possible factors 1 to x |
| x (in reverse) | int | Parameter: the number to reverse; its digits are removed one by one |
| rev | int | The reversed number built so far |
| x (in isAdam) | int | Parameter: the number being tested for Adam |
| sq | int | Square of x |
| r | int | Reverse of x |
| rsq | int | Square of the reverse r |
| i (in showNumbers) | int | Loop counter that runs from m to n |
| args | String[] | Command line arguments of main (not used) |
| sc | Scanner | Reads the input typed by the user |
| a, b (in main) | int | The lower and upper limit typed by the user |
| ob | PrimeAdam | Object that holds the range and calls the methods |
Sample input and output
Sample 1
Enter the lower limit m: 1
Enter the upper limit n: 100
The Prime-Adam integers are:
2 3 11 13 31
Frequency of Prime-Adam integers is: 5Sample 2
Enter the lower limit m: 100
Enter the upper limit n: 200
The Prime-Adam integers are:
101 103 113
Frequency of Prime-Adam integers is: 3Sample 3
Enter the lower limit m: 4
Enter the upper limit n: 10
The Prime-Adam integers are:
NIL
Frequency of Prime-Adam integers is: 0Sample 4
Enter the lower limit m: 0
Enter the upper limit n: 10
INVALID INPUTDry run
Input: $m = 12$, $n = 13$. The constructor sets m = 12, n = 13, freq = 0, and isValid() returns true. showNumbers() runs i = 12 and i = 13.
| i | isPrime(i) | r = reverse(i) | sq | rsq | reverse(sq) | Action | freq |
|---|---|---|---|---|---|---|---|
| 12 | false (factors 1, 2, 3, 4, 6, 12) | not tested | - | - | - | skipped | 0 |
| 13 | true (count = 2) | 31 | 169 | 961 | 961 | 961 equals 961, so print 13 | 1 |
showFrequency() then prints 1. The output is 13 followed by Frequency of Prime-Adam integers is: 1, the same as a real run of the program with these limits.
Variations
- Adam Number: "Accept a number and check whether it is an Adam number."
- Prime Palindrome: "Accept a number and check whether it is both prime and a palindrome."
- Emirp Number: "Accept a number and check whether it is a prime number whose reverse is also prime."
- Prime-Adam check for one number: "Accept a number and check whether it is a Prime-Adam number."