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

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 m...

Perfect number in java, program for perfect number in java

A perfect number is one where the sum of factors of a number (excluding the number itself) is equal to the number itself. EXAMPLE- We take 6. Factors of 6 are: 1,2,3 6 = 1 + 2 + 3 6 is a perfect number. Now we take 28 .Factors of 28 are: 1,2,4,7,14 28 = 1 + 2 + 4 + 7 + 14 28 is a perfect number. Perfect number in java language can be made by first applying a for loop starting from 1 to 1 less than that number and then with the help of if statement, checking if the number is divisible by that particular number which is currently stored in loop control variable.  Then after the loop terminates we will check whether the sum of factors of the number is equal to the number itself or not. If the statement returns true then appropriate statement prints. Here is the code for perfect number in two different formats firstly with the help of methods and secondly a very simple program using main method.