List All Prime Numbers problem

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
case 'B':// Display a list of all Prime numbers between 1-1000.
	{
		system ("cls");
		cout << "DISPLAYING ALL PRIME NUMBERS FROM 1-1000\n\n";
		int count=1;
		// 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.
				}
				else if (i == j + 1)
				{
					cout << "   " << i << "\t"; // Separate each number by a tab.

					if (count % 9 == 0) // Add CR after printing 8 numbers.
					{
						cout <<"\n";
					}
					count++;
				}
			}   
		}
		cout << "\n";
		system ("pause");
	}
	break;
Last edited on
You could do it like this. The important change is the new variable bool prime. It is set false at line 18 and tested at line 23.

Apologies, i also made some minor formatting changes to suit my own preferences.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int count=1;

    for (int i = 2; i < 1000; i++)
    {
        bool prime = true;

        for (int j = 2; j < i; j++)
        {
            if (i % j == 0)
            {
                prime = false;
                break; // i is NOT prime.
            }
        }

        if (prime)
        {
            cout << setw(5) << i;
            if (count % 10 == 0) // CR every 10 numbers.
            {
                cout <<"\n";
            }
            count++;
        }
    }
    cout << endl;

    cin.get();
    return 0;
}

Last edited on
OK, what does the cin.get(); statement do?

I added the bool statement and it still is doing the same thing? It lists all the prime numbers from 3 to 997.
Did you try the code exactly as in my previous post?
When I run it, this is the output:
    2    3    5    7   11   13   17   19   23   29
   31   37   41   43   47   53   59   61   67   71
   73   79   83   89   97  101  103  107  109  113
  127  131  137  139  149  151  157  163  167  173
  179  181  191  193  197  199  211  223  227  229
  233  239  241  251  257  263  269  271  277  281
  283  293  307  311  313  317  331  337  347  349
  353  359  367  373  379  383  389  397  401  409
  419  421  431  433  439  443  449  457  461  463
  467  479  487  491  499  503  509  521  523  541
  547  557  563  569  571  577  587  593  599  601
  607  613  617  619  631  641  643  647  653  659
  661  673  677  683  691  701  709  719  727  733
  739  743  751  757  761  769  773  787  797  809
  811  821  823  827  829  839  853  857  859  863
  877  881  883  887  907  911  919  929  937  941
  947  953  967  971  977  983  991  997


What does the cin.get(); statement do?
- that's an alternative to the use of system("pause") which is discussed here and elsewhere. The use of system() should be avoided.
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
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.
Topic archived. No new replies allowed.