+ 1

What are the differences between these two codes ? AND Why ?

#include <iostream> using namespace std; int main(){ int prime=1; int num; int p_num=2; cin>>num; if (num==0){cout<< "no prime numbers";} else{for (; p_num<=num; p_num++){ prime=1; for (int i=2; i<p_num; i++){ if (p_num%i==0) prime=0;} if (prime==1) cout <<p_num <<endl;} } return 0; } AND #include <iostream> using namespace std; int main(){ int prime=1; int num; int p_num=2; cin>>num; if (num==0){cout<< "no prime numbers";} else{for (; p_num<=num; p_num++){ prime=1; for (int i=2; i<p_num; i++){ if (p_num%i==0) prime=0;} if (prime=1) cout <<p_num <<endl;} } return 0; } Thnx for answering this untidy question.

18th Oct 2018, 7:54 PM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar
6 ответов
0
Hello friend. if (prime==1) AND if (prime=1) it's the only difference that I can find. in the == case the result is fine because the operation is going to evaluate prime equals to 1. in the = case I think that is wrong because he is going to make prime equals to 1 but that doesn't make sense because where is the if condition that you are going to evaluate? I hope that my answer help you. See ya!
18th Oct 2018, 8:04 PM
CosmiCat
CosmiCat - avatar
0
@FullCoder ACLHEMIST no, I'm saying that maybe the second one doesn't work.
18th Oct 2018, 9:38 PM
CosmiCat
CosmiCat - avatar
0
in the =case the statement has to work , for if it didn't , there will be no output, but we know that output is there....... you said 'the second one didn't work' what does that mean, the "if" condition or the statement in the curly braces ??
18th Oct 2018, 9:59 PM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar
0
the if statment
19th Oct 2018, 1:52 PM
CosmiCat
CosmiCat - avatar
0
but if the statement doesn't work how come we have an output ? so confused here... : ( CosmiCat ??
19th Oct 2018, 5:24 PM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar
- 1
CosmiCat so you're saying that in the = case, if makes prime = 1 and also evaluates the condition even if it's not true, because there IS no condition ...?
18th Oct 2018, 8:08 PM
FullCoder ALCHEMIST
FullCoder ALCHEMIST - avatar