Question

Here I have a C++ program that prints 2 to n prime numbers. The problem is, it is printing one digit more than what it needs to. Ex: n = 20 and it prints up to 23 instead of stopping at 19.

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

int main()
{
    int n, status = 1, num = 3, count, c;
    // User input to get n
    cout << "Enter n: ";
    cin >> n;

    // Input is a possible prime number
    if (n >= 1){
        // Print 2 as the first prime number
        cout << 2 << endl;
    }
    // loop that will iterate through n numbers
    for (count = 2 ; count <=n ;){
        // for each element check whether it is prime or not
        for (c = 2 ; c <= (int)sqrt(num) ; c++){
            // Not a prime number if divisble by something other than 1 or itself
            if (num%c == 0){
                status = 0;
                break;
            }
        }
        // Print prime numbers
        if (status != 0){
            cout << num << endl;
            count++;
            // If printed number is greater than n, stop the loop and printing
            if (num >= n){
                status = 0;
                break;
            }
        }
        status = 1;
        num++;
    }         

    return 0;
}
Was it helpful?

Solution

The problem lies with this section:

// Print prime numbers
if (status != 0){
    cout << num << endl;
    count++;
    // If printed number is greater than n, stop the loop and printing
    if (num >= n){
        status = 0;
        break;
    }
}

Since you check if num exceeds your end n only after you print the number, you'll end up printing one extra prime number. To fix this, you could simply rearrange your code a little, to only print after checking the condition:

// Print prime numbers
if (status != 0){
    // If number is greater than n, stop the loop and printing
    if (num > n){
        status = 0;
        break;
    }

    cout << num << endl;
    count++;
}

Note how I also changed the num >= n to just num > n. This will ensure that if n is a prime number, it will still be printed. (So the maximum of the range you specify will be inclusive.) Hope this helped! Let me know if you have any questions.

OTHER TIPS

This

    // Print prime numbers
    if (status != 0){
        cout << num << endl;
        count++;
        // If printed number is greater than n, stop the loop and printing
        if (num >= n){
            status = 0;
            break;
        }
    }
    status = 1;
    num++;

should be

    // Print prime numbers
    if (status != 0){
        cout << num << endl;
        count++;
        // If printed number is greater than n, stop the loop and printing
    }
    if (num >= n){
        status = 0;
        break;
    }
    status = 1;
    num++;

You should use "num" not "count" in loop condition.

while (num <= n){
        // for each element check whether it is prime or not
        for (c = 2 ; c <= (int)sqrt(num) ; c++){
            // Not a prime number if divisble by something other than 1 or itself
            if (num%c == 0){
                status = 0;
                break;
            }
        }
        // Print prime numbers
        if (status != 0){
            cout << num << endl;
        }
        status = 1;
        num++;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top