Emirp Number ISC Computer Science 2013 Theory
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 num): to assign n = num, 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 functions. Define main() function to create an object and call the methods to check for Emirp number.
class Emirp{
int n;
int rev;
int f;
public Emirp(int num){
n = num;
rev = 0;
f = 2;
}
public int isPrime(int x){
if(x < 2){
f = 2;
return 0;
}
if(x == f){
f = 2;
return 1;
}
if(x % f == 0 && f < x){
f = 2;
return 0;
}
f++;
return isPrime(x);
}
public void isEmirp(){
for(int i = n; i != 0; i /= 10)
rev = rev * 10 + i % 10;
if(isPrime(n) == 1 && isPrime(rev) == 1)
System.out.println("Emirp number!");
else
System.out.println("Not an emirp number.");
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = Integer.parseInt(in.nextLine());
Emirp obj = new Emirp(num);
obj.isEmirp();
}
}
Comments
Post a Comment