Question 4.12: Write a Java method to find all twin prime numbers less than 100.
Expected Output:
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
Solution:
package lab4problem12;
public class Lab4Problem12 {
public static void main(String[] args) {
twinPrimeNumber();
}
static void twinPrimeNumber() {
for (int i = 2; i < 100; i++) {
if (isPrime(i) && isPrime(i + 2)) {
System.out.println("(" + i + ", " + (i+2) + ")");
}
}
}
public static boolean isPrime(int n) {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0){
return false;
}
}
return true;
}
}