Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Accept the limits m and n. If m < 1 or m >= n, print INVALID INPUT and stop.
  2. Set freq = 0 and print the heading The Prime-Adam integers are:.
  3. For every number i from m to n:
    • count its factors in count, by trying every j from 1 to i; a prime has exactly two factors, so continue only if count == 2;
    • reverse i into r: copy i into x and, while x > 0, do r = r * 10 + x % 10 and x = x / 10;
    • find sq = i * i and rsq = r * r, and reverse sq into rs in the same way;
    • if rs == rsq, the square of the number and the square of its reverse are reverses of each other, so i is an Adam number as well: print i and add 1 to freq.
  4. If freq is 0, print NIL. 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
mintLower limit of the range typed by the user
nintUpper limit of the range typed by the user
freqintCount of Prime-Adam numbers found
iintThe number being tested; loop counter from m to n
countintNumber of factors of i
jintLoop counter that tries each possible factor of i
xintCopy of i whose last digit is dropped while reversing
rintThe reverse of i
sqintThe square of i
rsqintThe square of the reverse r
yintCopy of sq whose last digit is dropped while reversing
rsintThe 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: 5

Sample 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: 3

Sample 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: 0

Sample 4

Enter the lower limit m: 50
Enter the upper limit n: 10
INVALID INPUT

One value per line

Dry run

Input: $m = 12$, $n = 13$. The program prints 13 and frequency 1. The outer for loop runs for i = 12 and i = 13.

iprime?r (reverse of i)sqrsqrs (reverse of sq)Actionfreq
12false (factors 1, 2, 3, 4, 6, 12)not tested---skipped0
13true (count = 2)31169961961961 equals 961, so print 131

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

  1. Adam Number: "Accept a number and check whether it is an Adam number."
  2. Prime Palindrome: "Accept a number and check whether it is both prime and a palindrome."
  3. Emirp Number: "Accept a number and check whether it is a prime number whose reverse is also prime."
  4. Prime-Adam check for one number: "Accept a number and check whether it is a Prime-Adam number."