JAVA Program for Twin Prime Number
Twin Prime Number
A twin prime is a prime number that is either 2 less or 2 more than another prime number.
Example- (5,7), (11,13), (17,19).
In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair.
Java Program :
class TwinPrimes {
public static void main(String args[]) {
String primeNo = "";
int j = 0;
int LastPrime = 1;
System.out.println("Twin Primes are:");
for (int i = 1; i < 100; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
primeNo += i + " ";
if ((i - LastPrime) == 2) {
System.out.println("("+(i-2)+","+i+")");
}
LastPrime = i;
}
}
System.out.println("Prime Numbers are: " + primeNo);
}
}
Output
Comments
Post a Comment