Printing prime numbers between 3 and user input

Trying to make the program ask a user for a number and then the program is to print out all he prime numbers between 3 and the input number. This is printing but it is printing multiples of each prime number. I don't know what I am doing wrong. This is the hint the tutor gave me:

The problem is in the inner loop. You need to move the cout statement out of that loop. The loop control cannot be num either. Think of what the outer loop represents. Then it should be easy to fix the problem.
Give it another shot.

I have moved it to many different places but still nothing works right. Please help.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// PrimeNumbersTry6.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int num;
	bool isPrime;
	int counter = 0;
	int outerloop;

	cout << setw(5) << " " << "Enter a Number: ";
	cin >> num;
	

	for (int i = 3; i <= num; i++)
	{
		for (int j = 2; j < num; j++)
		{

			if (i % j == 0)
			{
				isPrime = false;
				break;
			}

			else
			{
				isPrime = true;
			}//end inner else

		if (isPrime)
		{
			cout << setw(5) << i;
			counter++;
			if (counter == 10)
			{
				cout << endl;
				counter = 0;

			} //end inner if


		} //end if

		}// end inner for

	}//end outer for

	
return 0;
}

yes, its your loops and conditions and nesting logic.

the logic is
for all the numbers, check each number if prime, if its prime, print it.
you have
for all the numbers, for (check if prime and if prime print)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  you want (pseudocode)

for(all the numbers)
{
     isprime = true;  //simplifies logic
    for(2 to sqrt(num) //this is less work, you do too many iterations
      {
          if(num%i ==0) 
             isprime= false;  //else is redundant
      }
     if(isprime)  //this is in the outer loop, not the inner as you have it. 
      print(num)
}


Let me chew on this and see if I can figure it out. I will post if I am just that lost. I am ready to cry. Been messing with this for days and still can't get it to work right.
Thank you for the input. I managed to get it done by using a code I found on this site and then figuring out how it worked and where I was going wrong with it.
Topic archived. No new replies allowed.