Twin Primes
A pair of prime numbers that differ by 2 (successive odd numbers that are both Prime numbers).Examples: (3,5), (5,7), (11,13), ...
import java.util.*;
class twin_prime
{
public static void main(String args[])
{
int n1,n2,i,c1=0,c2=0,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers");
n1=sc.nextInt();
n2=sc.nextInt();
for(i=1;i<=n1;i++)
{
if(n1%i==0)
c1++;
}
for(i=1;i<=n2;i++)
{
if(n2%i==0)
c2++;
}
d=Math.abs(n1-n2);
if(d==2 && c1==2 && c2==2)
System.out.println("Twin prime");
else
System.out.println("Not Twin prime");
}
}
Output:
Enter Two numbers:
5
7
Twin prime