Java program of emirp number
Answers
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
Answer:
import java.util.*;
class emirp
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("enter the integer");
int n = sc.nextInt(),f=0,cpy=n,rev=0,c=0;
for (int i=1;i<=n;i++)
{if(n%i==0)
c++;
}
if(c==2)
{
while(n>0)
{int ld=n%10;
rev=rev*10+ld;
n=n/10;
}
System.out.println("rev="+rev);
}
for (int j=1;j<=rev;j++)
{if(rev%j==0)
f++;
}
if(f==2)
System.out.println("it is an emirp number");
else
System.out.println("it is not an emirp number");
}
}