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

/**
* The class TwinPrime print all the twin prime number less than 1000
*/
  
import java.io.*;
class TwinPrime
{ //function for checking whether a number is prime or not
boolean prime(int n)
{
int count=0;
for(int i=1; i<=n/2; i++)
{
if(n%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));
System.out.println(\"nThe Twin Prime Numbers within the given range 1000 are : \");
for(int i=1; i<=(1000-2); i++)
{
           //calling of function within 1000
if(ob.prime(i) == true && ob.prime(i+2) == true)
{
System.out.print(\"(\"+i+\",\"+(i+2)+\") \");
                   System.out.print(\"\ \");
}
}

  

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site