tags:

views:

161

answers:

2
int main()
{
 int i,j;

 for (i=1; i<=25; i++)
 {
  for (j=2; j<= i/2; j++)
   if (!(i%j)) break;
  if (j>i/2) cout << i << "\n";
 }
 return 0;
}

This program (not written by me) outputs the prime numbers from 1 to 25, including 1 even though 1 isnt prime.

I am having trouble with this line: if (!(i%j)) break;

Does this say "not modulus of i and j = 0?

+14  A: 

!(i%j) is the same as (i%j)==0, or "i is divisible by j"

Ned Batchelder
+3  A: 

The two following lines are essentially identical (as far as logic goes):

if (!(i%j))
if ((i % j) == 0)

The way I'd read the first line to make it clearer is "if there is not a remainder from i/j", ie, i is divisible by j.

Matthew Scharley