WRITE A JAVA PROGRAM Twin primes Twin primes are a pair of p
WRITE A JAVA PROGRAM
(Twin primes) Twin primes are a pair of prime numbers that differ by 2. For example,
3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are twin primes.
Write a program to find all twin primes less than 1,000. Display the output as follows:
(3, 5)
(5, 7)
.....
...
..
.
Solution
class TwinPrime
{ int check=0;
public isPrime(int n)
{
for(i=1;i<=n;i++)
{
if(n%i == 0)
check++;
if(check==2)
return true;
else
return false;
}
}
public static void main(String args[])
{
//We have the range here till 1000 so no need to ask user.
for(i=1;i<=1000;i++)
{
if(isPrime(i) && isPrime(i+2))
{System.out.println(\"(\" + i + \",\" + i+2+\")\");
}
}
}
}

