SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Emirp Number Program in Java and Python

02 April 2013

ISC 2013 Question 8 solution to design an Emirp class and check whether a number and its reverse are both prime using recursion.

Question:

An Emirp number is a number which is prime backwards and forwards. For example, 13 and 31 are both prime numbers. Thus, 13 is an Emirp number.

Design a class Emirp to check if a given number is an Emirp number or not. Some of the members of the class are given below:

Class name: Emirp Data members / instance variables: n : stores the number rev : stores the reverse of the number f : stores the divisor Member functions: Emirp(int nn) : to assign n = nn, rev = 0 and f = 2 int isprime(int x) : to check if the number is prime using recursive technique and return 1 if prime, otherwise return 0 void isEmirp() : to reverse the given number and check if both the original number and the reverse number are prime by invoking isprime(int), then display the result with an appropriate message

Specify the class Emirp giving details of the constructor, int isprime(int) and void isEmirp(). Define the main() function to create an object and call the methods to check for an Emirp number.

Example 1

INPUT: 13 OUTPUT: 13 is an Emirp Number

Example 2

INPUT: 41 OUTPUT: 41 is Not an Emirp Number

Algorithm:

Step 1: Start.

Step 2: Define a class named Emirp.

Step 3: Declare integer instance variables n, rev and f.

Step 4: In the constructor, assign n = nn, rev = 0 and f = 2.

Step 5: In isprime(x), if n <= 1, return 0.

Step 6: If x > n / 2, return 1 because no divisor has been found.

Step 7: If n % x == 0, return 0 because n is divisible by x.

Step 8: Otherwise, return isprime(x + 1) to test the next divisor recursively.

Step 9: In isEmirp(), store a copy of the original number.

Step 10: Reverse the number by repeatedly extracting the last digit and adding it to rev.

Step 11: Call isprime(f) to check whether the original number is prime.

Step 12: Assign the reversed number to n, reset f to 2 and call isprime(f) again.

Step 13: If both returned values are 1, display that the original number is an Emirp Number.

Step 14: Otherwise, display that the original number is not an Emirp Number.

Step 15: In main(), accept a number, create an object and call isEmirp().

Step 16: Stop.

Explanation:

An Emirp number is a prime number whose reverse is also prime. The word itself is formed by reversing the word prime. For example, 13 is prime and its reverse, 31, is also prime. Therefore 13 is an Emirp number. The program must check two things: first, whether the original number is prime, and second, whether the reversed number is prime.

The class stores the original number in n, the reverse in rev and the divisor in f. The constructor initializes these values. The divisor starts from 2 because every number is divisible by 1, and divisibility by 1 does not help in deciding whether a number is prime. The prime checking method is recursive. It checks whether n is divisible by the current divisor x. If it is divisible, the number is not prime and the method returns 0. If the divisor becomes greater than half of n, no factor has been found, so the method returns 1.

The recursive step is important. If the current divisor does not divide the number, the method calls itself with x + 1 and returns that result. Returning the recursive result is necessary because the final answer may be found in a later call. This keeps the method recursive while allowing the result to come back correctly through the chain of calls.

The isEmirp() method first reverses the original number using digit extraction. The last digit is obtained using % 10 and added to rev. Then the number is shortened using integer division by 10. After reversing, the program checks the original number for primality. Then it places the reversed number in n, resets the divisor to 2 and checks again. If both the original number and its reverse are prime, the number is displayed as an Emirp number; otherwise, it is not.

Java Program:

Java
/**
* The class Emirp inputs a number and checks whether it is
* an Emirp number or not using recursive prime checking.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 8
*/

import java.util.Scanner;

class Emirp
{
    int n;
    int rev;
    int f;

    Emirp(int nn)
    {
        n = nn;
        rev = 0;
        f = 2;
    }

    int isprime(int x)
    {
        if(n <= 1)
            return 0;
        else if(x > n / 2)
            return 1;
        else if(n % x == 0)
            return 0;
        else
            return isprime(x + 1);
    }

    void isEmirp()
    {
        int original = n;
        int copy = n;

        // Reverse the original number.
        while(copy > 0)
        {
            int d = copy % 10;
            rev = rev * 10 + d;
            copy = copy / 10;
        }

        int a = isprime(f);

        // Check the reversed number using the same recursive method.
        n = rev;
        f = 2;
        int b = isprime(f);

        if(a == 1 && b == 1)
            System.out.println(original + " is an Emirp Number");
        else
            System.out.println(original + " is Not an Emirp Number");
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter any number : ");
        int n = sc.nextInt();

        Emirp ob = new Emirp(n);
        ob.isEmirp();
    }
}

Equivalent Python Program:

Python
# Program to check whether a number is an Emirp number.

class Emirp:
    def __init__(self, nn):
        self.n = nn
        self.rev = 0
        self.f = 2

    def isprime(self, x):
        # Recursive prime checking for self.n.
        if self.n <= 1:
            return 0
        elif x > self.n // 2:
            return 1
        elif self.n % x == 0:
            return 0
        else:
            return self.isprime(x + 1)

    def is_emirp(self):
        original = self.n
        copy = self.n

        # Reverse the original number.
        while copy > 0:
            d = copy % 10
            self.rev = self.rev * 10 + d
            copy = copy // 10

        a = self.isprime(self.f)

        # Check the reversed number using the same recursive method.
        self.n = self.rev
        self.f = 2
        b = self.isprime(self.f)

        if a == 1 and b == 1:
            print(original, "is an Emirp Number")
        else:
            print(original, "is Not an Emirp Number")


num = int(input("Enter any number : "))
ob = Emirp(num)
ob.is_emirp()

Output:

Enter any number : 13 13 is an Emirp Number Enter any number : 41 41 is Not an Emirp Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →