display the first n prime numbers

hello i am stuck and need some help. I am trying to make it so it displays the first n prime numbers (starting with 2 and n being user input)to the console but I'm only getting the range 2 through n and i can't seem to figure out how to change it.

code is as follows:
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
 #include <iostream>
using namespace std;


int main() {
    int num,i,count,n,isPrime;
    cout << "Enter a Number between 30 and 2000: ";
    cin >> n;
    count=0;
    for(num = 1; count<=n; num++){
        isPrime = 0;
        count++;
        for(i=2;i<=num/2;i++){
            if(num%i==0){
                isPrime++;
                break;
            }
        }

        if(isPrime==0 && num!= 1)
            cout << num << endl;
    }


    return 0;
}

for example if i put 32 for user input i get the range of primes 2-32. i need the first n or in this case 32 prime numbers not the range.
Last edited on
Your count variable is increasing every iteration in the for loop, no matter what. Once count is greater than n, your loop is done. And this will happen after n iterations, because count is increasing every iteration.

I would suggest only incrementing count after you've determined that the current num is a prime.
Last edited on
yeah i put it after the last if before the cout and it lists numbers not primes 1-131. so it stops where i want it to stop but also adds the non primes?

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
 
#include <iostream>
using namespace std;


int main() {
    int num, i, count=0, n, isPrime;
    cout << "Enter a Number between 30 and 2000: ";
    cin >> n;

    for (num = 2; count < n; num++) {
        isPrime = 0;


        for (i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime++;
                break;
            }
        }

        if (isPrime == 0 && num != 1)
            count++;
        cout << num << endl;


    }

  return 0;
}


and output to display is
Enter a Number between 30 and 2000: 32
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
now I'm not sure how to get rid of the non prime numbers?
Last edited on
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
#include <iostream>

int main()
{
    int n_primes ;
    std::cout << "Enter a Number between 30 and 2000: ";
    std::cin >> n_primes ;

    // adjust n_primes if it is out of range
    if( n_primes < 30 ) n_primes = 30 ;
    else if( n_primes > 2000 ) n_primes = 2000 ;

    std::cout << 2 << '\n' ; // the first prime number
    int count = 1 ; // we have already printed the first prime number

    // loop till count == n_primes (we have printed out n_primes prime numbers)
    for( int num = 3 ; count < n_primes ; num += 2 ) // for every odd number from 3 upwards
                                                     // (the only even prime number is 2)
    {
        bool is_prime = true ;

         // check for divisibility by odd numbers (we know that num is odd, not divisible by 2)
         for( int i = 3 ; i < num/2 ; i += 2 ) // up to the square root of num would suffice
                                               // (but we will ignore that nicety for now)
         {
             if( num % i == 0 )
             {
                 is_prime = false ; // divisible, not a prime.
                 break ; // not a prime; no need for any more trial divisions for this number
             }
         }

         if( is_prime ) // if we have found  a prime number
         {
             std::cout << num << '\n' ; // print it out
             ++count ; // and increment the count
         }

    }
}
I got this while waiting for a response which works flawlessly
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
 
#include <iostream>
using namespace std;
int main() {
 int i,j,n,count = 0;
 bool isPrime;
 int N;
    cout << "Enter a Number between 30 and 2000: ";
    cin >> N;
 while (N < 20 || N >  2000) {
    cout << endl;
    cout << "Invalid input" << endl;
    cout << "Please enter a valid Number between 30 and 2000: ";
    cin >> N;
    cout << endl; }
    n=0;
 for(i=2; n<20; i++){
    count++;

// Check if i is a prime, i.e.
// if it is divisible by one of the found primes
    isPrime = true;

  for(j=2; j<=i/2; j++){
   if(i%j == 0){
    isPrime = false;
     break;
     }
     }
   if(isPrime){
    n++;
   cout << "· Prime #" << "1" << " = " << i << endl;
            }
        }


    return 0;}

the only problem i have is where the 1 is at the bottom in the cout is that it must increase with the loop like i put 20 so the 1 must go prime # 1, prime # 2, prime #3 until either user input or 20 in the case of the code above.
the output is as follows:
Enter a Number between 30 and 2000: 32
· Prime #1 = 2
· Prime #1 = 3
· Prime #1 = 5
· Prime #1 = 7
· Prime #1 = 11
· Prime #1 = 13
· Prime #1 = 17
· Prime #1 = 19
· Prime #1 = 23
· Prime #1 = 29
· Prime #1 = 31
· Prime #1 = 37
· Prime #1 = 41
· Prime #1 = 43
· Prime #1 = 47
· Prime #1 = 53
· Prime #1 = 59
· Prime #1 = 61
· Prime #1 = 67
· Prime #1 = 71
i need them to count up by one but I'm not sure where to put count++ I've tried after the first for and the second for and after the first if and after and before the n++ but it won't work.

i would also like to create a function that checks if the number in the loop is prime or not and return a true or false. I'm not sure where to return it to or where/ how to call it in my main.
Last edited on
I'm a bit out-of-sync here since I based this code directly on that in the very first post at the start of this thread. It implements the change suggested by @Ganado http://www.cplusplus.com/forum/general/223101/#msg1022664
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
#include <iostream>
using namespace std;

int main()
{   
    int n;
    cout << "Enter a Number between 30 and 2000: ";
    cin  >> n;
    
    for (int num = 2, count = 0; count < n; num++)
    {
        bool isPrime = true;
        
        for (int i = 2; i<=num/2; i++)
        {
            if (num % i == 0)
            {
                isPrime = false;
                break;
            }
        }

        if (isPrime)
        {
            count++;
            cout << num << endl;
        }
    }   
}


If a separate function is used (which is a good idea) then the main loop would look like this:
10
11
12
13
14
15
16
17
    for (int num = 2, count = 0; count < n; num++ )
    {
        if ( isPrime(num) )
        {
            count++;
            cout << num << endl;
        }
    } 


Also to get the style of output mentioned in the previous post, replace the cout (in the code in this post) with
 
    cout << "Prime #" << count << " = " << num << endl;

Last edited on
okay but i am confused as to what the separate function will look like and what value to return. would it be a bool variable to return if it is a prime or not return a true or false? or an int?
Last edited on
Yes, the function will take a single integer (or better, unsigned integer) as a parameter, and return a bool. Internally it can use whatever you like, all that main() is concerned with is how it will call the function and act on the result.

For example the function could be declared like this:
 
    bool isPrime(unsigned int num);



alright i got this for my code and it asks me to input a number but won't do anything after that?
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
#include <iostream>
using namespace std;

bool isPrime(int k) {
    bool CheckP = false;

    for (int j = 2; j <= k / 2; j++) {
        if (k % j == 0) {
            CheckP = false;

            break;
        }

    return CheckP;}

 }
int main() {
    int N;
    cout << "Enter a Number between 30 and 2000: ";
    cin >> N;
    while (N < 20 || N >  2000) {
        cout << endl;
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number between 30 and 2000: ";
        cin >> N;
        cout << endl; }

    for (int i = 2, count = 0; count < N; i++ )
    {
        if ( isPrime(i) == true)
        {
            count++;
            cout << "· Prime #" << count << " = " << i << endl;
        }
    }


    return 0; }
actually reworked it and got this

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
#include <iostream>
using namespace std;

bool isPrime(int k) {
    bool CheckP = true;

    for (int j = 2; j <= k / 2; j++) {
        if (k % j == 0) {
            CheckP = false;

            break;
             } return CheckP;

    }

 }
int main() {
    int N;
    cout << "Enter a Number between 30 and 2000: ";
    cin >> N;
    while (N < 20 || N >  2000) {
        cout << endl;
        cout << "Invalid input" << endl;
        cout << "Please enter a valid Number between 30 and 2000: ";
        cin >> N;
        cout << endl; }

    for (int i = 2, count = 0; count < N; i++ )
    {
         if (isPrime(i) ){

            count++;
            cout << "· Prime #" << count << " = " << i << endl;}
        }



    return 0; }

the is the output though
Enter a Number between 30 and 2000: 32
· Prime #1 = 5
· Prime #2 = 7
· Prime #3 = 9
· Prime #4 = 11
· Prime #5 = 13
· Prime #6 = 15
· Prime #7 = 17
· Prime #8 = 19
· Prime #9 = 21
· Prime #10 = 23
· Prime #11 = 25
· Prime #12 = 27
· Prime #13 = 29
· Prime #14 = 31
· Prime #15 = 33
· Prime #16 = 35
· Prime #17 = 37
· Prime #18 = 39
· Prime #19 = 41
· Prime #20 = 43
· Prime #21 = 45
· Prime #22 = 47
· Prime #23 = 49
· Prime #24 = 51
· Prime #25 = 53
· Prime #26 = 55
· Prime #27 = 57
· Prime #28 = 59
· Prime #29 = 61
· Prime #30 = 63
· Prime #31 = 65
· Prime #32 = 67
its counting by two? starting at 5?
Last edited on
Your program probably gets stuck in an (effectively) infinite loop at line 28-35.

There are several problems in function isPrime(). The first is reported by the compiler:
[Warning] control reaches end of non-void function [-Wreturn-type]

That means there is at least a theoretical possibility that the function will reach the closing brace without encountering a return statement. In this case it is more than theoretical. Say k has the value 2. What value is received by the calling code in main? We don't know.

Also the function explicitly sets CheckP to false in two places, but it never sets it to true anywhere. So we know the function must return either a garbage value or false.
hmmm what about in the second code i attached? the one that gives me odds as my output starting with 5? Im sorry I'm very new to this and really have no idea why the separate function is not working. why is it giving me that warning?
Look at the loop in the isPrime() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool isPrime(int k) {
    bool CheckP = true;

    for (int j = 2; j <= k / 2; j++) {
        if (k % j == 0) {
            CheckP = false;

            break;
             } return CheckP;

    }

 }


It has an opening brace and a closing brace. The return statement is placed inside those braces, thus when the loop executes, it will always either break or return on the first iteration. When k is 2 or 3, the loop does not execute and thus there is no return statement and the same error of the garbage value. Likewise when the break is executed, there is no return statement, again garbage value.

But you want the loop to execute to completion. The number is prime only when none of the value of j is a factor. Therefore the return statement should be outside the loop body. Fortunately that fixes the other problem too. Now it will always return some specific value.
Last edited on
Topic archived. No new replies allowed.