Help with code

I need help making this code print the first n numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "std_lib_facilities.h"
bool Prime(int a)
{
    for(int c = 2; c <= a/2; c++)
    {
        if(a%c == 0)
        {
            return false;
        }
    }
    return true;
}
int main (){

    int b,c,d;
        bool prime2=true;
        int status = 1;

    vector<int>prime3;

    cout << "Enter a positive integer: ";
    cin >> b;
    if(b<=4) {
        cout<< "Error: Enter a number more than 3. \nEnter a positive integer: ";
        cin >> b;
        }

    while(c<=sqrt(b)){
        if(b%2 == 0)
            prime2 = false;
       c++;
    }

    if(prime2)
        cout << b << " is prime\n";
    else
        cout << b << " is not prime \n";

    if(b>=2)
        cout << "The first " << b << " primes are as follows:  \nprime [" << b << "] = 2";

       while(c<=b)
    {
        if(Prime(c))
        {
            prime3.push_back(c);
        }
        c++;
    }
            d=0;
    while(d<prime3.size())
        {
            if(status!=0)
            {

                if(prime3[d]) {
                cout << ",";

                cout << prime3[d];
                    
            }
                status =1;
                d++;
            }
        }
    cout << endl;
    return 0;
}


Right now if I enter 5 I receive 2, 3, 5 instead of 2, 3, 5, 7, 11

Thank you in advance
Last edited on
Printing the first n prime number would need a nested loop like this:
1
2
3
4
5
6
7
8
9
10
11
12
int p = 2;
cout << p;
for(int i = 0; i < (n - 1); ++i)
{
  ++p;
  while(not Prime(p))
  {
    ++p;
  }
  cout << "," << p;
}
    cout << endl;
How can I do that by using vectors?
Store the prime numbers in the vector until the size of the vector is the number of primes you want.
You have a number of logic errors in your program, in addition to it being overly complicated.

Line 22: b appears to be the number of desired primes. Yet at lines 28-32, you're attempting to see if b is prime.

Lines 34-37: You're displaying whether the number of requested primes is itself prime.

Lines 42-49: You don't reset c. You're also checking that c is less than the number of desired primes, while c is a prime candidate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <cmath>
using namespace std; 

bool Prime(int a)
{   for(int c = 2; c<=sqrt((double)a); c++)  // use sqrt(a), not a/2
    {   if (a%c == 0)
            return false;        
    }
    return true;
}

int main ()
{   int c;
    unsigned b;
    vector<int>prime3;

    cout << "Enter number of desired prime numbers: ";
    cin >> b;
    while (b<4)    //  This should be a loop, not an if statement
    {   cout<< "Error: Enter a number more than 3. \nEnter a positive integer: ";
        cin >> b;
    }

    c = 3;    //    First prime
    while (prime3.size() < b)
    {   if (Prime(c))
        {   cout << c << " is prime\n";
            prime3.push_back (c);       //  Add to vector
        }            
        else
            cout << c << " is not prime \n";
        c++;            
    }
    cout << "The first " << b << " primes are:  " << endl; 
    for (unsigned i=0; i<prime3.size(); i++) 
        cout << prime3[i] << " ";
    cout << endl;
    system ("pause");
    return 0;
}

Last edited on
Sometimes it helps if you write your plan down in plain English like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
Read number of primes from the user.

Create a vector to hold the primes.

set number to 3 - first prime

while size of vector is smaller than the number of primes
  if number is a prime
     add it to the vector
     increment number by 2 // skip even numbers
end while

Finally print all the numbers in the vector
Topic archived. No new replies allowed.