Tuesday , 27 June 2017
Home » Class 12 » [Question 8] ISC 2013 (Theory) Paper Solved – Emirp Number

[Question 8] ISC 2013 (Theory) Paper Solved – Emirp Number

Question:

An Emirp number is a number which is prime backwards and forwards. 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 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) : check if the number is prime using the recursive technique and return 1 if prime otherwise return 0
void isEmirp() : reverse the given number and check if both the original number and the reverse number are prime, by invoking the function isprime(int) and display the result with an appropriate message.

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

Programming Code:

/**
* The class Emirp inputs a number and checks whether it is an Emirp number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 8
*/
 
import java.util.*;
class Emirp
{
    int n, rev, f;
    Emirp(int nn) //parameterised constructor
    {
        n=nn;
        rev=0;
        f=2;
    }
    
    /* Note: The function isPrime(...) will check whether 'n' is prime or not
     * It will not check whether 'x' is prime or not.
     * The variable 'x' is just a counter to increment the recursion process
     */
    int isprime(int x)
    {
        if(x<=n) 
        {
            if(n%x!=0)
            {
                isprime(x+1);
            }
        } 
        if(x==n)
            return 1;
        else
            return 0;
    }
     
    void isEmirp()
    {
         int copy=n, d;
         while(copy>0) // code for reversing a number
         {
             d=copy%10;
             rev=rev*10+d;
             copy=copy/10;
         }
     
         int a=isprime(f); //checking whether the Original number 'n' is Prime or not
         n = rev; //saving reverse in 'n' so that function isprime() checks it to be prime
         f=2; //resetting the value of f for checking whether the reverse number is Prime or not
         int b=isprime(f); //checking whether the Reverse number is Prime or not
     
         if(a==1 && b==1) //If both Original and Reverse are Prime, then it is an Emirp number
         System.out.println("It is an Emirp Number");
         else
         System.out.println("It is Not an Emirp Number");
    }
     
    public static void main(String args[])throws Exception
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter any number : "); //inputting the original number
        int n=sc.nextInt();
        Emirp ob=new Emirp(n);
        ob.isEmirp();
    }   
}

Output:

Enter any number : 13
13 is an Emirp Number

Enter any number : 41
41 is Not an Emirp Number

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

Leave a Reply

Your email address will not be published. Required fields are marked *