Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

A prime number is a number that has only two factors, 1 and itself. 
An Emirp number is a number, that when reversed, gives a prime number. 
Example: 13, when reversed gives 31, which is a prime number.
Note: Emirp numbers are not palindromes.
Palindromes are numbers, which when reversed , give the same number. 
Example: 131 when reversed, given 131. 
Hence, 131 is a palindrome.
131 is not an emirp.

Emirp Number

Algorithm

  1.   Start
  2. n:=0
  3. temp:=0
  4. rev=0
  5. digit:=0
  6. i:=0
  7. k:=0
  8. Print "Enter a number"
  9. Read n
  10. temp=n
  11. If temp>0, go to step 12 or else go to step 16
  12. digit=temp%10
  13. rev=rev*10+digit
  14. temp=temp/10
  15. Go to step 11
  16. If step 17==true && n!=rev, then go to step 26 or else go to step 27
  17. k=0. Go to step 18
  18. i=2. Go to step 19
  19. If i<n, go to step 20 or else go to step 23
  20. If rev%i==0, go to step 21 or else go to step 22
  21. k++. Go to step 22
  22. i++. Go to step 19
  23. If k==0, go to step 24 or else go to step 25
  24. true. Go to step 17
  25. false. Go to step 17
  26. Print "The given number is an Emirp Number". Go to step 28
  27. Print "The given number is not an Emirp Number".
  28. Stop

Program

Buffered Reader

import java.io.*;
class emirp
{
    int n, temp, rev=0, digit, i, k;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter a number");
        n=Integer.parseInt(br.readLine());
    }
    
    void reverse()
    {
        temp=n;
        while(temp>0)
        {
            digit=temp%10;
            rev=rev*10+digit;
            temp=temp/10;
        }
    }
    
    boolean isPrime()
    {
        k=0;
        for(i=2;i<rev;i++)
        {
            if(rev%i==0)
            {
                k++;
            }
        }
        if(k==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void display()
    {
        if(isPrime()==true && n!=rev)
        {
            System.out.println("The given number is Emirp Number");
        }
        else
        {
            System.out.println("The given number is not Emirp Number");
        }
    }
    
    void main() throws IOException
    {
        accept();
        reverse();
        display();
    }
}

Scanner

import java.util.*;
class emirp
{
    int n, temp, rev=0, digit, i, k;
    
    void accept() 
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter a number");
        n=sc.nextInt();
    }
    
    void reverse()
    {
        temp=n;
        while(temp>0)
        {
            digit=temp%10;
            rev=rev*10+digit;
            temp=temp/10;
        }
    }
    
    boolean isPrime()
    {
        k=0;
        for(i=2;i<rev;i++)
        {
            if(rev%i==0)
            {
                k++;
            }
        }
        if(k==0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void display()
    {
        if(isPrime()==true && n!=rev)
        {
            System.out.println("The given number is Emirp Number");
        }
        else
        {
            System.out.println("The given number is not Emirp Number");
        }
    }
    
    void main() 
    {
        accept();
        reverse();
        display();
    }
}

Documentation

Variable Description

Data Type Variable NameDescription
 int nTo store number entered by the user. 
 inttemp To act as a temp variable 
 intdigit To store the digits of the number 
 intrev To store the reverse of a number 
 intTo act as a counter variable 
 intTo store the number of factors of the number other than 1 and itself. 

Method Description

Return TypeMethod NameDescription
 voidaccept() To accept data from the user 
 voidreverse() To reverse the number  
 boolean isPrime() To check if the reversed number is prime or not 
 voiddisplay() To display whether the given number is emirp or not 
 void main() To act as an entry gateway for the compiler  

Output


Emirp Number Output




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful


Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Complex Sentences to Compound Sentences