your reply helped me a lot as it shew me the meaning beyond
prime=true
but I am still confused so I will sum up things and make myself more clear and specific
we will take the code piece by piece :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
int main(){
int num;
bool prime;
for(int i = 3; i <= 10; i++){
prime = true;
for(int n = 2; n <= i - 1; n++){
if(i % n == 0){
prime = false;
}
}
if(prime){
cout << i << " is prime" << endl;
}
}
return 0;
}
|
the two loops :
the first loop gives(3,4,5,6,7,8,9,10)
the second loop gives (3, 4,4,5,5,5,6,6,6,6,7,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10)
then
the operation(i % n == 0) is made on each number and every time the condition if(i % n == 0) is met the number is eliminated according to
1 2 3
|
if(i % n == 0){
prime = false;
}
|
what remains after are these numbers:
3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10)
why 6,6 and 8,8... and 10 , 10 ....
because:
6%4 is not equal 0 true
6%5 is not equal 0 true
8%7 is not equal 0 true
8%3 is not equal 0 true
10%7 is not equal 0 true
10%6 is not equal 0 true
the condition says that only the numbers that met:
1 2
|
if(i % n == 0){
prime = false;
|
will be false or eliminated like:
10%5=0 false
10%2=0 false
6%3=0 false
6%2=0 false
but
6%4 is not equal 0 so true
10%4 is not equal 0 so true
and so on ....
so the result will be :
3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10)
then comes the last part :
1 2 3
|
if(prime){
cout << i << " is prime" << endl;
}
|
so if prime is true , it means if prime is not false , it means if i%n is not equal zero , it means this code
1 2
|
if(i % n == 0){
prime = false;
|
so cout << i << " is prime" << endl;
therefore it should give this result:
3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10) and not (3,5,7)!!!!!!!!!!???
I need an explanation about all what I wrote
after your reply KESKIVERTO , I understand that prime=true inside the first for loop has a role that I did not get before but now become clear , thanks to you, its role when combined with the for loop is to decide , everytime the loop scans , if the number is true namely if the number % n is not equal zero , namely if the number is not a multiple of another .
but you did not explain HOW it does it , so I would appreciate if you show me with details how ????
and finally
if the prime=true inside the for loop would look if number is a multiple or not and following your sentence:
" Now we can ask whether any of them answered "yes" to the "is it a multiple?". The answer is in the variable 'prime'."
everytime , the first loop scans , the numbers
(3,4,5,5,5,6,6,7,7,7,7,7,8,8,8,8,9,9,9,9,9,9,10,10,10,10,10,10) are controled by
prime= true
the result should be:
(3,5,5,5,7,7,7,7,7) and not (3,5,7)
how what is the problem????
and especially this :
everytime the loop scans , if the number is true namely if the number % n is not equal zero , namely if the number is not a multiple of another it will be kept otherwise it would be eliminated . and the numbers kept will be printed in the screen
you did not explain as I wrote above HOW the mechanism of
prime=true
inside the first loop is done , so I would appreciate if you show me with details
how ????