Computer Science, asked by Tushar30218, 1 month ago

Write a Java Program to input two numbers and check they are twin prime or not.
(Two numbers are said to be Twin Prime if both numbers are prime and their difference is 2.)

I just want to see how many can answer by using just 1 loop.

Answers

Answered by suryawolverine17
1

Answer:

import java.io.*;

public class TwinPrimeNumber

{

public static boolean isPrime(int n)

{

boolean f = true;

for (int i = 2; i <= n / 2; i++) {

if (n % i == 0) {

f = false;

break;

}

}

return f;

}

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

{

int number1, number2;

InputStreamReader in = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(in);

System.out.print("Enter first number: ");

number1 = Integer.parseInt(br.readLine());

System.out.print("Enter second number: ");

number2 = Integer.parseInt(br.readLine());

the difference between two is 2

if (isPrime(number1) == true && isPrime(number2) == true && Math.abs(number2 - number1) == 2) {

System.out.println("Twin prime number");

} else {

System.out.println("Not twin prime numbers");

}

}

}

Similar questions