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

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

  1. main reads a and b, then creates the object with new PrimeAdam(a, b). The constructor copies them into the data members m and n and sets freq to 0.
  2. isValid() returns true only when m is at least 1 and less than n. If it returns false, main prints INVALID INPUT.
  3. Otherwise main calls showNumbers(), which loops i from m to n and for each i checks isPrime(i) and isAdam(i). Every number that passes is printed and freq is increased by 1. NIL is printed if none is found.
  4. main then calls showFrequency() to print freq.
  5. isPrime(x) counts the factors of x from 1 to x; it is prime if the count is 2.
  6. reverse(x) repeatedly appends the last digit (rev = rev * 10 + x % 10) and removes it (x = x / 10) until x is 0.
  7. isAdam(x) finds sq, r and rsq (the squares and the reverse) and returns true if reverse(sq) equals rsq.

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

VariableData TypePurpose
mintData member: lower limit of the range
nintData member: upper limit of the range
freqintData member: count of Prime-Adam numbers found
a, b (in constructor)intParameters of the constructor: the lower and upper limit
x (in isPrime)intParameter: the number being tested for prime
countintNumber of factors of x found so far
i (in isPrime)intLoop counter that runs through possible factors 1 to x
x (in reverse)intParameter: the number to reverse; its digits are removed one by one
revintThe reversed number built so far
x (in isAdam)intParameter: the number being tested for Adam
sqintSquare of x
rintReverse of x
rsqintSquare of the reverse r
i (in showNumbers)intLoop counter that runs from m to n
argsString[]Command line arguments of main (not used)
scScannerReads the input typed by the user
a, b (in main)intThe lower and upper limit typed by the user
obPrimeAdamObject 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: 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: 0
Enter the upper limit n: 10
INVALID INPUT

One value per line

Dry 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.

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

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

  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."