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.
import java.io.*;
class Emirp
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n, rev, f;
Emirp(int nn) //parameterised constructor
{
n=nn;
rev=0;
f=2;
}
int isprime(int x) //recursive function for checking whether 'x' is Prime or not
{
if(f<=x)
{
if(x%f!=0) // f is increasing everytime we are not getting any factor
{
f++;
isprime(x);
}
}
/* Since, 'f' was increasing everytime we did not get any factor,
* Hence, if 'f' becomes equal to the number, then that means that the number
* is not divisible by any other number except 1 and itself
* Hence it is Prime */
if(f==x)
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(n); //checking whether the Original number is Prime or not
f=2; //resetting the value of f for checking whether the reverse number is Prime or not
int b=isprime(rev); //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(n+" is an Emirp Number");
else
System.out.println(n+" is Not an Emirp Number");
}
public static void main(String args[])throws IOException
{
System.out.print("Enter any number : "); //inputting the original number
int n=Integer.parseInt(br.readLine());
Emirp ob=new Emirp(n);
ob.isEmirp();
}
}
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.
import java.io.*;
class Emirp
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n, rev, f;
Emirp(int nn) //parameterised constructor
{
n=nn;
rev=0;
f=2;
}
int isprime(int x) //recursive function for checking whether 'x' is Prime or not
{
if(f<=x)
{
if(x%f!=0) // f is increasing everytime we are not getting any factor
{
f++;
isprime(x);
}
}
/* Since, 'f' was increasing everytime we did not get any factor,
* Hence, if 'f' becomes equal to the number, then that means that the number
* is not divisible by any other number except 1 and itself
* Hence it is Prime */
if(f==x)
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(n); //checking whether the Original number is Prime or not
f=2; //resetting the value of f for checking whether the reverse number is Prime or not
int b=isprime(rev); //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(n+" is an Emirp Number");
else
System.out.println(n+" is Not an Emirp Number");
}
public static void main(String args[])throws IOException
{
System.out.print("Enter any number : "); //inputting the original number
int n=Integer.parseInt(br.readLine());
Emirp ob=new Emirp(n);
ob.isEmirp();
}
}