In the above C++ program to prints a twin prime that has the maximum size among twin primes less than or equal to the given number, we need to understand that A twin prime is a prime number that is either 2 less or 2 more than another prime number—for example, either member of the twin prime pair (41, 43). In other words, a twin prime is a prime that has a prime gap of two.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const int num = 10005;
bool prime[num];
for (int i = 2; i != num; ++i) {
prime[i] = true;
}
for (int i = 2; i != int(sqrt(num)); ++i) {
if (prime[i]) {
for (int j = 2; i * j < num; ++j) {
prime[i*j] = false;
}
}
}
int n;
cout << "Input an integer:\n";
cin >> n;
cout << "Twin primes of the entered number are :\n";
for (int i = n; i - 2 >= 0; --i) {
if (prime[i] && prime[i-2]) {
cout << i-2 << " " << i << endl;
break;
}
}
return 0;
}
Sample Output: Input an integer: 6 Twin primes of the entered number are : 3 5