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

Max Twin Prime Below n

Write a C++ program that reads the integer n and prints a twin prime that has the maximum size among twin primes less than or equal 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".

Visual 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> // Including input-output stream header file
#include <cmath>    // Including math functions header file
using namespace std; // Using the standard namespace

int main() { // Start of the main function
    const int num_primes = 10005; // Constant to define the maximum limit for finding primes
    bool primes[num_primes]; // Declaring an array to store prime flags for numbers up to num_primes

    // Initializing the prime number flags to true for all indices except 0 and 1
    for (int i = 2; i != num_primes; ++i) {
        primes[i] = true;
    }

    // Loop to sieve out non-prime numbers using the Sieve of Eratosthenes algorithm
    for (int i = 2; i != int(sqrt(num_primes)); ++i) {
        if (primes[i]) { // Checking if the current number is marked as a prime
            // Marking all multiples of the current prime as non-prime
            for (int j = 2; i * j < num_primes; ++j) {
                primes[i * j] = false;
            }
        }
    }

    int n;
    cout << "Input an integer:\n"; // Asking the user to input an integer
    cin >> n; // Reading the input from the user

    cout << "Twin primes are:\n"; // Displaying twin primes

    // Loop to find and display twin primes starting from the given input number
    for (int i = n; i - 2 >= 0; --i) {
        if (primes[i] && primes[i - 2]) { // Checking if the current number and its preceding number are both prime
            cout << i - 2 << " " << i << endl; // Displaying the twin primes
            break; // Exiting the loop after finding the first twin primes
        }
    }

    return 0; // Indicating successful completion of the program
}

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:

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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/cpp-exercises/basic/cpp-basic-exercise-62.php