I wrote the following code to output a list of all prime numbers between 1 and 1000. It is not outputting 2 though. I understand why. The initial value of both j and i are 2 and since j is not less than i at that time, it does not show it.
Other than adding a cout << "2\t";
How can I get my code to kick out the 2, which is also prime?
case'B':// Display a list of all Prime numbers between 1-1000.
cout << "DISPLAYING ALL PRIME NUMBERS FROM 1-1000\n\n";
// cout << " 2\t";
for (int i = 2; i < 1000; i++)
for (int j = 2; j < i; j++)
// cout << "j = " << j << endl;
if (i % j == 0)
break; // i is NOT prime. Move on to the next number.
elseif (i == j + 1)
cout << " " << i << "\t"; // Separate each number by a tab.
if (count % 9 == 0) // Add CR after printing 8 numbers.
cout << "\n";
Chervil, that works perfectly when I use your code exactly. Now that I read the attached thread, I understand the system issue. What I don't understand is that at the bottom of your code, you have cin.get(); and you said it should replace the system ("pause");. However, it does not. I read the other thread and they said to do cin.get(); cin.get();. When I use that, it works, but leaves no message on the screen telling the user what to do. I added a cout statement and resolved that.