What is Emirp Number In Java

In this section, we will learn what is an emirp number and also create Java progrm to check if the given number is emirp or not. The emirp number Java program frequently asked in Java coding tests to check the logic of the programmer.

Emirp Number

A number is called an emirp number if we get another prime number on reversing the number itself. In other words, an emirp number is a number that is prime forwards or backward. It is also known as twisted prime numbers.

Note: Palindrome primes are excluded.

Emirp Number Example

Suppose, we want to check the number 79 is emirp or not.

We know that 79 is a prime number means that divisible by 1 and self only. On reversing the number, we get 97 which is another prime number. Therefore, 79 and 97 both are prime numbers. Hence, 79 is a prime number. Similarly, we can check other numbers also.

Some other emirp numbers are 13, 199, 107, 113, 1399, 1583, 1201, 3049, etc.

Steps to find Emirp Number

  1. Read or initialize a number (n).
  2. First, check the given number (n) is prime or not.
    • If not, break the execution and exit.
    • If prime, find the reverse (r) of the given number (n).
  3. Check the reverse number (r) is prime or not.
    • If not, print number (n) is not emirp.
    • If prime, print the given number (n) as an emirp number.

Let’s implement the above logic in a Java program.

Emirp Number Java Program

EmirpNumberExample1.java

  1. import java.io.*;  
  2. import java.util.*;  
  3. public class EmirpNumberExample1   
  4. {  
  5. //function that checks the given number is prime or not  
  6. public static boolean isPrime(int n)  
  7. {  
  8. //base case  
  9. if (n <= 1)  
  10. return false;  
  11. //loop executes from 2 to n-1  
  12. for (int i = 2; i < n; i++)  
  13. if (n % i == 0)  
  14. //returns false if the condition returns true  
  15. return false;  
  16. //returns true if the condition returns false  
  17. return true;  
  18. }  
  19. //function that checks if the given number is emirp or not  
  20. public static boolean isEmirp(int n)  
  21. {  
  22. //checks if the given number is prime or not  
  23. if (isPrime(n) == false)  
  24. return false;  
  25. //variable that stores the reverse of the number  
  26. int reverse = 0;  
  27. //the while loop executes until the specified condition becomes false  
  28. while (n != 0)   
  29. {  
  30. //finds the last digit of the number (n)  
  31. int digit = n % 10;  
  32. //finds the reverse of the given number  
  33. reverse = reverse * 10 + digit;  
  34. //removes the last digit  
  35. n = n/10;  
  36. }  
  37. //calling the user-defined function that checks the reverse number is prime or not  
  38. return isPrime(reverse);  
  39. }  
  40. //driver code  
  41. public static void main(String args[])   
  42. {  
  43. Scanner sc=new Scanner(System.in);  
  44. System.out.print(“Enter a number to check: “);  
  45. //reading an integer from the user  
  46. int n=sc.nextInt();  
  47. if (isEmirp(n) == true)  
  48. System.out.println(“Yes, the given number is an emirp number.”);  
  49. else  
  50. System.out.println(“No, the given number is not an emirp number.”);  
  51. }  
  52. }  

Output 1:

Enter a number to check: 1399
Yes, the given number is an emirp number.

Output 2:

Enter a number to check: 14
No, the given number is an emirp number.

Leave a Comment