This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.
But I don\'t think tha
It's fine to change your for loop to for (int j=2; j<=sqrt(i); j++)
but then you also need to change something else. Looking specifically at your print condition,
else if (i == j+1) {
cout << i << " ";
}
why will that never be triggered if you only iterate up to sqrt(i)
? Where can you move the cout
to to change this? (Hint: you may want to move the print out of the loop and then make use of some type of flag variable)