Java Program to find Emirp Number

Khurshid Md Anwar
2 min readAug 16, 2020

--

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

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.

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

13

31

Number is Emirp

Enter the number

14

41

Number is not Emirp

Enter the number

71

17

Number is Emirp

Originally published at https://javaknowhow.blogspot.com.

--

--

Khurshid Md Anwar
Khurshid Md Anwar

Written by Khurshid Md Anwar

Computer Science Trainer and blogger

No responses yet