Thursday, 28 January 2016

Emirp number check

Emirp Number:
An emirp number is a number which is prime backwards and forwards.
Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp to check if a given number is Emirp number or not.

Code:
import java.util.*;
class Emirp
{
  public static void main(String args[])
  {
    Scanner sc=new Scanner(System.in);
    char ch;
    do{
int i,c=0,r,n2=0,c1=0;
    System.out.println("Enter The Number: ");
    int n=sc.nextInt();
   
    for(i=1;i<=n;i++)
    {
      if(n % i == 0)
        c++;
    }
    if(c == 2)
    {
      r = n % 10;
      n2 = (n2 * 10) + r;
      n = n / 10;
    }
    for(i=1;i<=n2;i++)
    {
      if(n2 % i == 0)
        c1++;
    }
    if ( c ==2 && c1 ==2)
    {
          System.out.println("***Emirp Number***\n");
    }
    else
    {
          System.out.println("***Not Emirp Number***\n");
    }
    System.out.println("Do You Want To Check More Numbers ? Press y / n: ");
    ch=sc.next().charAt(0);
    }
    while(ch!='n');
    System.exit(0);
    }
}
Output:
Sample Output
Input the number
13
31
Number is Emirp

Input the number
17
71
Number is Emirp

Input the number
14
41
Number is not Emirp

5 comments:

  1. checked your code...not showing 31 as emirp

    ReplyDelete
  2. //My program is working you can check it out
    import java.util.*;
    class Emirp
    {public static void main(String args[])
    {int m,a,t=0,c=0,ct=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter a no for Emirp test");
    int n=s.nextInt();
    m=n;
    while(n>0)
    { a=n%10;
    t=t*10+a;
    n=n/10;
    }
    for(int i=1;i<=m;i++)
    {if(m%i==0)
    c++;
    }
    for(int i=1;i<=t;i++)
    {if(t%i==0)
    ct++;}
    if(c==2&&ct==2)
    System.out.println("emirp no");
    else
    System.out.println("not");
    }}

    ReplyDelete
  3. MY PROGRAM IS WORKING YOU CAN CHECK IT OUT
    import java.util.*;
    class Emirp
    {public static void main(String args[])
    {int m,a,t=0,c=0,ct=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter a no for Emirp test");
    int n=s.nextInt();
    m=n;
    while(n>0)
    { a=n%10;
    t=t*10+a;
    n=n/10;
    }
    for(int i=1;i<=m;i++)
    {if(m%i==0)
    c++;
    }
    for(int i=1;i<=t;i++)
    {if(t%i==0)
    ct++;}
    if(c==2&&ct==2)
    System.out.println("emirp no");
    else
    System.out.println("not");
    }}

    ReplyDelete
  4. Thanks ayush bandhopadhay!!
    Ur program helped me a lot to understand emirp number

    ReplyDelete
  5. import java.util.*;
    public class Emirp {
    public static void EmirpNumber () { //
    //reverse = (reverse * 10) + lastDigit
    Scanner sc = new Scanner (System.in);
    System.out.println("Enter a number");
    int num = sc.nextInt();
    int digits,reverse=0,temp1=num;
    int k=1,i=1,c1=0,c2=0;
    while (num!=0) {
    digits=num%10;
    reverse = (reverse * 10) + digits;
    num/=10;
    }
    while (i<=temp1) {
    if (temp1%i==0)
    c1++;
    i++;
    }
    while(k<=reverse) {
    if (reverse%k==0)
    c2++;
    k++;
    }
    if (c1==2 && c2== 2)
    System.out.print("Emirp number");
    else
    System.out.print("Not an Emirp number");
    sc.close();
    }
    }

    ReplyDelete