Computer Science, asked by khalidahmad7273, 1 year ago

Write A user defined fuction in java to accept a no. And check it is twin prime no. Or not

Answers

Answered by sushiladevi4418
0

Answer:

Write a user-defined function in java to accept a number And check it is twin prime number Or not.

Explanation:

import java.io.*;

class TwinPrimeRange

{

boolean isPrime(int n) //funton for checking prime

{

int count=0;

for(int i=1; i<=n; i++) {

if(n%i == 0) count++;

}

if(count == 2)

return true;

else return false;

}

public static void main(String args[]) throws IOException {

TwinPrimeRange ob = new TwinPrimeRange();

BufferedReader br=new BufferedReader(newInputStreamReader(System.in));

System.out.print("Enter the lower range : ");

int p = Integer.parseInt(br.readLine());

System.out.print("Enter the upper range : ");

int q = Integer.parseInt(br.readLine());

if(p>q)

System.out.println(“Invalid Range !”);

else

{

System.out.println(“nThe Twin Prime Numbers within the given range are : “);

for(int i=p; i<=(q-2); i++) {

if(ob.isPrime(i) == true && ob.isPrime(i+2) == true) { System.out.print("("+i+","+(i+2)+") "); }

}

}

}

}

Similar questions