Java Program to Check Emirp Number

In the previous article, we have discussed Java Program to Check Autobiographical Number

In this article we are going to understand what Emirp number is(Emirp is prime spelled backwards) and how we can check whether a number is Emirp number or not in Java with examples.

Program to Check Emirp Number

Emirp numbers are numbers who digits when reversed result in another prime number.

 Example :

17:   17 Is prime as well as 71; Emirp number
13:   13 Is prime as well as 31; Emirp number
19:   19 Is prime but not 91;    Not Emirp number

In the above examples the numbers 17 and 13 are Emirp numbers as their reverse numbers are also primes. However as 91 is not a prime number, 19 is not an Emirp number.

Let’s see different ways to check Emirp number.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Approach :

  1. We ask the user to enter a number and store it .
  2. We pass the number into a function that returns the reversed number, then both the number and the reversed number is checked for prime.
  3. If both of the numbers are prime then the number is said to be an Emirp number.

Method-1: Java Program to Check Emirp Number By Using Static Value

import java.util.Scanner;

public class EmirpNumber
{
    public static void main(String args[])
    {
        //A number declared
        int num = 17;

        if((isPrime(num)) && (isPrime(reverseNum(num))))
        {
            System.out.println(num+" is an Emirp number");
        }
        else
        {
            System.out.println(num+" is Not an Emirp number");
        }

    }

    //method returns reverse of a number
    static int reverseNum(int num)
    {
        int rev=0, remainder = 0;
        while (num != 0)
        {
            
            remainder = num % 10;
            rev = rev * 10 + remainder;
            num = num / 10;
        }
        
        return rev;
    }
    
    // method to check for prime
    static boolean isPrime(int num)
    {
        int iter = 2;
        boolean flag = true;
        while (num > iter)
        {
            if (num % iter == 0)
            {
                flag = false;
                break;
            }
            iter++;
        }
        return flag;
    }
}
Output:

17 is an Emirp number

Method-2: Java Program to Check Emirp Number By User Defined Method

import java.util.Scanner;

public class EmirpNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        if((isPrime(num)) && (isPrime(reverseNum(num))))
        {
            System.out.println(num+" is an Emirp number");
        }
        else
        {
            System.out.println(num+" is Not an Emirp number");
        }

    }

    //method returns reverse of a number
    static int reverseNum(int num)
    {
        int rev=0, remainder = 0;
        while (num != 0)
        {
            
            remainder = num % 10;
            rev = rev * 10 + remainder;
            num = num / 10;
        }
        
        return rev;
    }
    
    // method to check for prime
    static boolean isPrime(int num)
    {
        int iter = 2;
        boolean flag = true;
        while (num > iter)
        {
            if (num % iter == 0)
            {
                flag = false;
                break;
            }
            iter++;
        }
        return flag;
    }
}
Output:

Case-1

Enter a number : 17
17 is an Emirp number

Case-2

Enter a number : 23
23 is Not an Emirp number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: