print 20 prime numbers bigger than 6

Please, where is the mistake?

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
  int prime(int d){
int e=0;

do
{

for (int i=2;i<d;i++){

if (d%i==0){e=e;}
else{e+=1;}
d+=1;
cout<<e<<endl;

}

}
while(e!=0);
return d;
}

int main(){
int c=5;
cout<<prime(c);
return 0;
}
closed account (30X1hbRD)
Hmmm, just a guess but #include <iostream> and using namespace std; isn't there
First let's write a very naive primality test.
http://en.wikipedia.org/wiki/Primality_test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_prime(unsigned int n)
{
    if (n == 0 || n == 1)
        return false;

    if (n == 2)
        return true;

    for (unsigned int i=2; i < n; ++i)
        if (n % i == 0)
            return false;

    return true;
}


Now all you need to do is use the test to find 20 prime numbers, greater than 6.

This could be written like:

1
2
3
4
5
6
7
8
9
10
unsigned int n=6;

while (/* we don't have 20 numbers */)
{
    if (is_prime(n))
        // add n to list of primes (or print it)
        // numbers found += 1

    ++n:
}
d is a junk value.
e = e; Why is that in there?
I still find your formatting horrible and confusing and it makes it difficult to see errors tbh. The whole code just looks wrong and like a mess. It would also be helpful if you said what errors you were getting during runtime or compilation.

Anyway, you should do this:

Declare an integer equal to 7.
Check if all integers 2 up to (n - 1) divide n.
If none of them do, print the number. Increment a counter of how many numbers were printing.
Increment n.
Continue doing this until you have printed 20 numbers.
Hello!
Printing numbers is not enough.

The problem is,I have to save them in an array as elements.
The same functin is then computing the average value of the 20elements and returns that value with return.

I do not know how to do it.

We have only example that array is filled in main, not in function.

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

int Prime (int array_[5], int st) {
int array_f[5];
int m=0;
int k=0;

do
{




int e=1;
for (int i=2; i<st; i++){
if (st%i==0)
{
e=0;
break;
}


}


if(e==1){array_f[k]=st;}
k+=1;
m+=st/5;
st+=1;


}// do
while(k==4);
return m;
}

int main(){
int b=23;
cout<<Prime(b);
return 0;
}
Topic archived. No new replies allowed.