Twin Prime No.

How to write a program to accept 2 numbers and check whether they are twin primes or not?


/**
* The class TwinPrime inputs 2 numbers and checks whether they are twin primes or not
*/
  
import java.io.*;
class TwinPrime
{
     
/* Function isPrime( ) returns 'true' when the number 'x' is Prime and 'false' if it is not. */
boolean isPrime(int x)
{
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
count++;
}
if(count==2)
return true;
else
return false;
}
  
public static void main(String args[]) throws IOException
{
TwinPrime ob = new TwinPrime();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int m,n;
int c=0;
System.out.print("Enter the 1st number = ");
m=Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd number = ");
n=Integer.parseInt(br.readLine());
  
boolean a = ob.isPrime(m);
boolean b = ob.isPrime(m);
c = Math.abs(m-n);
if(b == true && b == true && c == 2)
  System.out.println("The numbers are Twin Primes");
else
  System.out.println("The Numbers are Not Twin Primes");
}
}

No comments:

Post a Comment