prime numbers up to five

Please, why is here result 5? It should count prime numbers up to 5...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  int Count(){
int q=1;
for (int n=3; n<=5; n++){


            for (int t=2;t<n;t++){
                                    if (n%t==0){break;}
                                   
                                        else{ q+=1;}
                                               
                                             
                                  } 
                         
                             }

return (q);
                }
int main(){
cout<<Count();
return 0;
}
You are incrementing q every time n is not divisible by t. So for n = 3 you will increment q when t is 2, and for n = 5 you will increment q when t is 2, 3, and 4. Since q is initialized to 1, you get 5.
Topic archived. No new replies allowed.