ISC Practicals and Theory - Prime Adam Number
Updated 21 Oct 2026
A Prime-Adam number is a positive integer that is both a prime number and an Adam number. This program, written in the main method alone, accepts two limits $m$ and $n$ (with $m \ge 1$ and $m < n$), displays every Prime-Adam number from $m$ to $n$ (both included) and prints how many there are.
Definition
- Prime number: a number that has exactly two factors, 1 and the number itself. Example: 13 (factors 1 and 13).
- Adam number: a number for which the square of the number and the square of its reverse are reverses of each other.
- Prime-Adam number: a number that is both prime and an Adam number.
Example: $13$ is prime. Its reverse is $31$. $13^2 = 169$ and $31^2 = 961$, and $169$ and $961$ are reverses of each other. So 13 is a Prime-Adam number.
Example of a prime that is not Adam: $17$. $17^2 = 289$, $71^2 = 5041$, but the reverse of $289$ is $982$, not $5041$.
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 (it has only one factor), so it is not a Prime-Adam number.
Algorithm
- Accept the limits
mandn. Ifm < 1orm >= n, printINVALID INPUTand stop. - Set
freq = 0and print the headingThe Prime-Adam integers are:. - For every number
ifrommton:- count its factors in
count, by trying everyjfrom 1 toi; a prime has exactly two factors, so continue only ifcount == 2; - reverse
iintor: copyiintoxand, whilex > 0, dor = r * 10 + x % 10andx = x / 10; - find
sq = i * iandrsq = r * r, and reversesqintorsin the same way; - if
rs == rsq, the square of the number and the square of its reverse are reverses of each other, soiis an Adam number as well: printiand add 1 tofreq.
- count its factors in
- If
freqis 0, printNIL. Then print the frequency.
Program
The outer for loop in main takes each number i. The first inner for loop counts its factors (the prime test) and the two while loops reverse i and its square.
import java.util.Scanner;
/* Program PrimeAdam displays the Prime-Adam numbers from m to n.
A Prime-Adam number is a number that is both prime and an Adam number. */
public class PrimeAdam
{
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 m = sc.nextInt(); // lower limit typed by the user
System.out.print("Enter the upper limit n: ");
int n = sc.nextInt(); // upper limit typed by the user
if (m < 1 || m >= n) // m must be positive and less than n
System.out.println("INVALID INPUT");
else
{
int freq = 0; // count of Prime-Adam numbers found
System.out.println("The Prime-Adam integers are:");
for (int i = m; i <= n; i++)
{
int count = 0; // number of factors of i
for (int j = 1; j <= i; j++)
{
if (i % j == 0) // j divides i, so j is a factor
count++;
}
if (count == 2) // a prime has exactly two factors
{
int x = i, r = 0; // r is the reverse of i
while (x > 0)
{
r = r * 10 + x % 10; // append last digit of x to r
x = x / 10; // drop the last digit of x
}
int sq = i * i; // square of the number
int rsq = r * r; // square of the reverse
int y = sq, rs = 0; // rs is the reverse of sq
while (y > 0)
{
rs = rs * 10 + y % 10;
y = y / 10;
}
if (rs == rsq) // reverse of sq must equal rsq: an Adam number
{
System.out.print(i + " ");
freq++;
}
}
}
if (freq == 0)
System.out.print("NIL");
System.out.println();
System.out.println("Frequency of Prime-Adam integers is: " + freq);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
m | int | Lower limit of the range typed by the user |
n | int | Upper limit of the range typed by the user |
freq | int | Count of Prime-Adam numbers found |
i | int | The number being tested; loop counter from m to n |
count | int | Number of factors of i |
j | int | Loop counter that tries each possible factor of i |
x | int | Copy of i whose last digit is dropped while reversing |
r | int | The reverse of i |
sq | int | The square of i |
rsq | int | The square of the reverse r |
y | int | Copy of sq whose last digit is dropped while reversing |
rs | int | The reverse of sq |
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: 50
Enter the upper limit n: 10
INVALID INPUTDry run
Input: $m = 12$, $n = 13$. The program prints 13 and frequency 1. The outer for loop runs for i = 12 and i = 13.
| i | prime? | r (reverse of i) | sq | rsq | rs (reverse of 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 |
Reversing 13: x = 13, rev = 0; then rev = 3, x = 1; then rev = 31, x = 0; the loop stops and r is 31. Reversing 169 gives 961 in the same way.
The loop ends, freq is 1, so the program prints 13 and 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."