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.
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.