primes/logic error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
using namespace std;
int main ()
{
for (int i=2; i<100; i++)
{
bool prime=true;
for (int j=i; j>1; j--)
{
if (i % j == 0)
{
prime=false;
break;
}
}
if(prime) cout << i << " ";
}
return 0;
}
| |
Hello- Can somebody help me find the error in the above code? It's supposed to list all primes up to 100.
I can get it to work if i change the 2nd loop to
but I fail to see why the above code fails. Thank you in advance
If j = i just occurred, then j%i == 0 is definitely true.
Maybe try j = i-1
Thanks :)
Topic archived. No new replies allowed.