Skip to main content

Emirp number in java, program to check emirp number

Emirp is simply the reverse prononciation of word "prime". An emirp number is the number which is prime and whose reverse is also a prime number.


EXAMPLE-1
17 
Reverse of the number = 71
Both the numbers are prime so 17 is an emirp number.
EXAMPLE-2
37
Reverse of the number = 73
Both the numbers are prime so 37 is an emirp number.
EXAMPLE-3
13
Reverse of the number = 31
Both the numbers are prime so 13 is an emirp number.

Emrip number can be easily made in java with the help of methods/functions.
Firstly, take an input of number, then check it is prime or not, then reverse the number and again check it is prime or not, if both are true, print the suitable output.

ALGORITHM-
STEP 1 : start
STEP 2 : print "enter a number"
STEP 3 : take input of number in e
STEP 4 : int n=num, int rev =0
STEP 5 : repeat STEP to STEP until n>0
STEP 6 : int d=n%10
STEP 7 : rev=(rev*10)+d
STEP 8 : n=n/10
STEP 9 :  int p= input value from STEP 13
STEP 10 : int d=0, x=num
STEP 11 : int i=2, repeat STEP 12 until i<=x/2, i++
STEP 12 : if (x/i==0) then d=1; break
STEP 13 : return d
STEP 14 : input value in q from STEP 18
STEP 15 :int d=0, x=rev
STEP 16 : int i=2, repeat STEP 12 until i<=x/2, i++
STEP 17 : if (x/i==0) then d=1; break
STEP 18 : return d
STEP 19 : if (p==1&&q==1) then goto STEP 20 else goto STEP 21
STEP 20 : print num+" is an emirp number"
STEP 21 : print num+" is not an emirp number"
STEP 22 : end

JAVA CODE-
import java.util.*;
public class emrip

{
    int num;
    int rev;
    emrip()
    {
    }
    emrip(int nn)
    {
        num=nn;
    }
    int isPrime(int x)
    {
        int d=0;
        for(int i=2;i<=x/2;i++)
        {
            if(x%i==0)
            {
            d=1;
            break;
           }
        }
        return d;
    }
    void isEmrip()
    {
        int n=num;
        rev=0;
        while(n>0)
         }
            int d=n%10;
            rev=(rev*10)+d;
            n=n/10;
         }
        emrip obj=new emrip();
        int p=obj.isPrime(num);
        int q=obj.isPrime(rev);
        if(p==1&&q==1)
        System.out.println(num+" is an emirp number");
        else
        System.out.println(num+" is not an emirp number");
    }
    public static void main(String[] arg)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter a number");
        int e=sc.nextInt();
        emrip obj=new emrip(e);
        obj.isEmrip();
    }   
}


output-

Enter a number
37
37 is an emirp number

SIMILAR JAVA CODES-

If you find helpful please share

Comments

Popular posts from this blog

Palindrome number in java

A number is said to be palindrome if it reads both forward and backwards same. Example- 175571 454 A program for palindrome number can be made by simply extracting digits of the number in reverse order and adding them with their place value and then checking is it same as the original number or not. To extract a digit in revers order and adding with place value we simply follow the below code. while(n>0) { int d=n%10; rev=(rev*10)+d; n=n/10; } To explain this let us take an example 657 Then  For first iteration =  n=657                                     d=657%10=7                                     rev=(0*10)+7=7 For second iteration = n=65                                         ...

Java program for prime numbers

A prime number is the number which is is  divisible by itself and not by other numbers. A program to check prime number can be made by using for and while loop combination . This article contains java program for finding prime number ,write a java program for prime number between 1- 100 ,simple program to find prime factorial of a number in java. EXAMPLE 1 - program to check whether a number is prime number  or not. import java.util.*; public class pr { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter number"); int num=sc.nextInt(); int d=0; for(int i=2;i<=num;i++) { if(num%i==0) d++; } if(d==0) System.out.println("it is a prime number"); else System.out.println("it is not a prime number"); } } Output enter number 23 it is a p...

What is magic number in java ?

A number is said to be magic number if the repetitive sum of digits of number is equal to 1 or a single digit number . If the repetitive sum ends up  as 1 then the number is a magic number, else not. For example- n= 289;.   2+8+9 = 19      19;.      1+9 = 10      10;.      1+0= 0       1 Since the number ends in 1, hence 289 is a magic number. Magic number in java is the java program to check whether a number is magic number or not . For this we do repetitive addition of the digits of the number till the number is a two or more digit number. If after repetitive adding digits of the number ends up to 1 then the loop stops and a message is displayed. For this program we need to extract digits of the number for this we use while loop and add this digit . Code snippet to get digits of the number - while(n!=0) {        int d = n%10;         s=s+d;   ...