Java program for Emirp Number
A number which is prime backward and forwards is called emirp number. 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
reverse stores the reverse of the number
f stores the divisor
Class name Emirp
Data members/instance variables:
n stores the number
reverse stores the reverse of the number
f stores the divisor
Member functions :
Emirp(int n) to assign n = n, reverse = 0 and f = 2
int isprime(int x) check if the number is prime using the recursive technique and return I 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(illt), int isprime(int) and void isEmirp().
Define the main( ) function to create an object and call the methods to check for Emirp number.
Emirp(int n) to assign n = n, reverse = 0 and f = 2
int isprime(int x) check if the number is prime using the recursive technique and return I 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(illt), 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.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* class Emirp
* ISC 2013 paper Question 08
*/
public class Emirp
{
// instance variables
private int n; // For number
private int reverse; // reverseeres number
private int temp; // temporary variable
/**
* Constructor for objects of
class Emirp
*/
public Emirp(int n)
{
// initialise instance
variables
this.n = n;
reverse = 0;
temp = 2;
}
public int isPrime(int x, int
i)
{
if(i==1){
return 1;
}
else
{
if(x%i==0)
return 0;
else
return
isPrime(x,i-1);
}
}
void isEmirp()
{
int a,p1, p2;
temp = n;
while(n !=0 )
{
a = n % 10;
reverse = reverse * 10
+a;
n = n/10;
}
System.out.println(reverse);
p1 =
isPrime(temp,temp/2); // call
the isPrime for the original number
p2 = isPrime(reverse,reverse/2); // call the isPrime for the reverse number
if( p1 == 1 &&
p2 == 1)
{
System.out.println("Number is Emirp");
}
else
{
System.out.println("Number is not Emirp");
}
}
public static void main(String
args[])throws IOException
{
int n;
InputStreamReader in=new
InputStreamReader(System.in);
BufferedReader br=new
BufferedReader(in);
System.out.println("Enter the number ");
n =
Integer.parseInt(br.readLine());
Emirp ob = new Emirp(n);
ob.isEmirp();
}
}
Sample Output
Enter the number
Enter the number
13
31
Number is Emirp
Enter the number
14
41
Number is not Emirp
Enter the number
71
17
Number is Emirp
Sl. No.
|
Variable Name
|
Data Type
|
Purpose
|
1
|
n, reverse, temp
|
int
|
instance variables
|
2
|
Emirp()
|
Constructor | The constructor of Emirp class
|
3
|
isPrime(int, int)
|
int
|
Checking prime number or not (recursive method)
|
4
|
isEmirp()
|
void
|
Checking Emirp
|
5
|
a, p1, p2
|
int
|
Local variable to isEmirp() method
|
6
|
in
|
InputStreamReader
|
The object of InputStreamReader class which exists in java.io
package
|
7
|
br
|
BufferedReader
|
Use to create object br which exists in java.io package
|
main()
|
void
|
Main function
|
More Program
Neon number program in java
Java program for Evil Number
More Java program
Java program for Selection Sort
Loops in Java - Java loop explained with examples