How we can print all twin prime numbers less then 100 in java??
Answers
Answered by
2
Explanation:
import java.util.Scanner;
public class Exercise16 {
public static void main(String[] args) {
for (int i = 2; i < 100; i++) {
if (is_Prime(i) && is_Prime(i + 2)) {
System.out.printf("(%d, %d)\n", i, i + 2);
}
}
}
public static boolean is_Prime(long n) {
if (n < 2) return false;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) return false;
}
return true;
}
}
Similar questions