if(insprime)
cout << x << " is prime. \n";
}
cout << '\n';
system("PAUSE");
return 0;
}
Everything works correct. But if I do the expression with my hand, if x is 1 and y is 2 ok, but if x is 2 and y is 2 because y<=2/2, so y doesn't become 3, the result should be 2%2 == 0 so insprime = false. Instead this program shows me number 2 too as a prime number. Sorry for my english but I'm italian xD. Anyway anyone can made me clear this "error"?
In doubt if I'm crazy or not I verified that my calculates were right with a program that shows me the modulus.
when x = 2, y = 2, y<=x/2 (2<=1) returns false so program never enters that for loop.
Note that this loop only has one statement in it. This would be easier to notice, if your code was properly indented. insprime was set to be true in a line above so it remains true in the line below.
Right. Thefor(y=2; y<=x/2; y++) executes no iterations until x >= 4. I think your confusion came from expecting if((x%y)==0) insprime = false; to execute for x=2, which it will not do.