Check if given number is Emirp Number or not

An Emirp Number (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed. This definition excludes the related palindromic primes.

Examples :

Input : n = 13
Output : 13 is Emirp!
Explanation :
13 and 31 are both prime numbers. 
Thus, 13 is an Emirp number.

Input : n = 27
Output : 27 is not Emirp.

Objective : Input a number and find whether the number is an emirp number or not.

Approach : Input a number and firstly check if its a prime number or not. If the number is a prime number, then we find the reverse of the original number and check the reversed number for being prime or not. If the reversed number is also prime, then the original number is an Emirp Number otherwise it is not.

Below is the implementation of above approach :.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if given
// number is Emirp or not.
#include <iostream>
using namespace std;
  
// Returns true if n is prime.
// Else false.
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Function will check whether number
// is Emirp or not
bool isEmirp(int n)
{
    // Check if n is prime
    if (isPrime(n) == false)
        return false;
  
    // Find reverse of n
    int rev = 0;
    while (n != 0) {
        int d = n % 10;
        rev = rev * 10 + d;
        n /= 10;
    }
  
    // If both Original and Reverse are Prime,
    // then it is an Emirp number
    return isPrime(rev);
}
  
// Driver code
int main()
{
    int n = 13; // Input number
    if (isEmirp(n) == true)
        cout << "Yes";
    else
        cout << "No";
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if given number is
// Emirp or not.
import java.io.*;
class Emirp {
    // Returns true if n is prime. Else
    // false.
    public static boolean isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;
  
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
  
        return true;
    }
  
    // Function will check whether number
    // is Emirp or not
    public static boolean isEmirp(int n)
    {
        // Check if n is prime
        if (isPrime(n) == false)
            return false;
  
        // Find reverse of n
        int rev = 0;
        while (n != 0) {
            int d = n % 10;
            rev = rev * 10 + d;
            n /= 10;
        }
  
        // If both Original and Reverse are Prime,
        // then it is an Emirp number
        return isPrime(rev);
    }
  
    // Driver Function
    public static void main(String args[]) throws IOException
    {
        int n = 13; // Input number
        if (isEmirp(n) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if 
# given number is Emirp or not.
  
# Returns true if n is prime. 
# Else false.
def isPrime( n ):
      
    # Corner case
    if n <= 1:
        return False
      
    # Check from 2 to n-1
    for i in range(2, n):
        if n % i == 0:
            return False
      
    return True
  
# Function will check whether
# number is Emirp or not
def isEmirp( n):
      
    # Check if n is prime
    n = int(n)
    if isPrime(n) == False:
        return False
          
        # Find reverse of n
    rev = 0
    while n != 0:
        d = n % 10
        rev = rev * 10 + d
        n = int(n / 10)
          
          
    # If both Original and Reverse 
    # are Prime, then it is an
    # Emirp number
    return isPrime(rev)
  
# Driver Function
n = 13 # Input number
if isEmirp(n):
    print("Yes")
else:
    print("No")
      
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if given
// number is Emirp or not.
using System;
  
class Emirp {
    // Returns true if n is prime
    // Else false.
    public static bool isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;
  
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
  
        return true;
    }
  
    // Function will check whether number
    // is Emirp or not
    public static bool isEmirp(int n)
    {
        // Check if n is prime
        if (isPrime(n) == false)
            return false;
  
        // Find reverse of n
        int rev = 0;
        while (n != 0) {
            int d = n % 10;
            rev = rev * 10 + d;
            n /= 10;
        }
  
        // If both Original and Reverse are Prime,
        // then it is an Emirp number
        return isPrime(rev);
    }
  
    // Driver Function
    public static void Main()
    {
        int n = 13; // Input number
        if (isEmirp(n) == true)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if given 
// number is Emirp or not.
  
// Returns true if n 
// is prime else false
  
function isPrime($n)
{
    // Corner case
    if ($n <= 1)
        return -1;
  
    // Check from 2 to n-1
    for ($i = 2; $i < $n; $i++)
        if ($n % $i == 0)
            return -1;
  
    return 1;
}
  
// Function will check 
// whether number is
// Emirp or not
function isEmirp($n)
{
    // Check if n is prime
    if (isPrime($n) == -1)
        return -1;
  
    // Find reverse of n
    $rev = 0;
    while ($n != 0)
    {
        $d = $n % 10;
        $rev = $rev * 10 + $d;
        $n /= 10;
    }
          
    // If both Original and 
    // Reverse are Prime, 
    // then it is an Emirp number
    return isPrime($rev);
}
  
// Driver code
$n = 13; 
  
if (isEmirp($n) ==-1)
    echo "Yes";
    else
    echo "No";
  
// This code is contributed by ajit
?>

chevron_right



Output :

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.