Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Prints a twin prime which has the maximum size among twin primes less than or equals to n

C++ Basic: Exercise-62 with Solution

Write a C++ program to which reads an given integer n and prints a twin prime which has the maximum size among twin primes less than or equals to n.

According to wikipedia "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".

Pictorial Presentation:

C++ Exercises: Prints a twin prime which has the maximum size among twin primes less than or equals to n

Sample Solution:

C++ Code :

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    const int num_primes = 10005;
    bool primes[num_primes];
    for (int i = 2; i != num_primes; ++i) {
        primes[i] = true;
    }

    for (int i = 2; i != int(sqrt(num_primes)); ++i) {
        if (primes[i]) {        
            for (int j = 2; i * j < num_primes; ++j) {
                primes[i*j] = false;
            }
        }
    }
    int n;
	cout << "Input an integer:\n";
    cin >> n;
	cout << "Twin primes are:\n";
        for (int i = n; i - 2 >= 0; --i) {
            if (primes[i] && primes[i-2]) {
                cout << i-2 << " " << i << endl;
                break;
            }
    }

    return 0;
}

Sample Output:

Input an integer:
Twin primes are:
11 13

Flowchart:

Flowchart: Prints a twin prime which has the maximum size among twin primes less than or equals to n

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to swap first and last digits of any number.
Next: Write a C++ program which prints three highest numbers from a list of numbers in descending order.

What is the difficulty level of this exercise?