how to break just the inner loop?

Hello!
I just want to count all prime numbers from -3 to 3.
Result, regarding this program (which is logical but not mathematical at moment) is 1, but I want it to be 7. (to include -3,-2,-1,0,1,2,3)

In other words, I just want the inner loop to stop, if the number is prime.
Please, how can I achieve that?
Can I have function ina function?
Many thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 int Count(){
int q=0;
for (int i1=-3; i1<=3; i1++){



for (int i2=2; i2<i1; i2++){

if (i1%i2!=0){
q+=1;
break;
}
                            }
                             }

return (q);
                }
A prime number is defined as being a number >1. Why even bother to check numbers below 1? The very fact they are lower than 1 makes them not prime.

I also find that your code is has an extremely strange and hard to read layout and I'm not sure your for loops are doing what you want. To check a number, n, is prime, just test if n is not divisible by integers from 2 up to sqrt(n) and if it is, it's prime.
Last edited on
Topic archived. No new replies allowed.