# Thread: Help with finding only prime numbers with for loops

1. ## Help with finding only prime numbers with for loops

So I'm new at all of this programming jazz and I could really use some help. The assignment has already been turned in, I would just like to figure this out before we move in class. Teacher isn't in town so I can't ask him for the moment. Could really use your help. Thanks.

So this is the assignment...

A prime number is a number greater than 1 which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 2 to n (where n is a user-specified number) are prime.
Ask the user for a number, n, greater than 2. Keep asking for a number until a number greater than 2 is provided. Assume that the user will only enter numbers (that is, you do not need to check if a user enters text).
Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime".
Use the modulus operator, %, to determine if a number is prime
Example output:

Please enter a number larger than two: -939
Please enter a number larger than two: 2
Please enter a number larger than two: 20
2 Prime
3 Prime
5 Prime
7 Prime
11 Prime
13 Prime
17 Prime
19 Prime

This is what I have so far. I suppose i'm not really understanding how to continue with the second for loop. The first one just prints out a list of numbers from 2 to the number designated by the user. In my case that variable is user_input.

Code:
```// Alen
// Prime
//

# include <iostream>
using namespace std;
int main()
{

int user_input;
int i;
int n = 1;
int total=2;

do {
cout << "Enter a number greater than 2." << endl;
cin >> user_input;
} while (user_input<=2);

for (i=2; i < user_input; i++)
{
}

return 0;

}```

2. > The assignment has already been turned in
If you've posted what you turned in, you just got either D- or F.

> Use a loop to iterate on a variable, i, from 2 through n.
OK, you've done this

> For each iteration, check all numbers from 2 through i
Now, what would you do here?

> Use the modulus operator, %, to determine if a number is prime
Or this

Have you looked on say wikipedia to see how the simple prime number algorithm works?
There's even a pretty animation.

3. > The assignment has already been turned in
If you've posted what you turned in, you just got either D- or F.
>I'm not disputing that.

> Use a loop to iterate on a variable, i, from 2 through n.
OK, you've done this
> Cool.
> For each iteration, check all numbers from 2 through i
Now, what would you do here?
> I have no idea.
> Use the modulus operator, %, to determine if a number is prime
Or this
> This % doesn't help if I don't know where to place it/ how to use it to get required results.

Have you looked on say wikipedia to see how the simple prime number algorithm works?
There's even a pretty animation.

4. I want to modulate the user_input by every number less than the value of user_input. I just don't understand how to code it.

5. > For each iteration, check all numbers from 2 through i
You mean like
for ( candidatePrime = 2 ; candidatePrime < i ; candidatePrime++ )

6. Could you explain how that differs from what I have up top?
for (i=2; i < user_input; i++)

Essentially all that does is list the numbers from 2 to n.

How do I go about dividing each of the listed numbers; by every number in the list that is less than it?

7. It differs because the instructions read

> Use a loop to iterate on a variable, i, from 2 through n. For each iteration (of i from 2 to n), check all numbers from 2 through i
Means your loops look like this
Code:
```for (i=2; i < user_input; i++) {
for ( j = 2 ; j < i ; j++ ) {
// now trial divide i with each j in turn
}
}```

8. So we needed another variable with the same value as i ?

I'm not sure I understand...

Code:
```// Alen
// Prime
//

# include <iostream>
using namespace std;
int main()
{

int user_input;
int i;
int n = 1;
int total;

do {
cout << "Enter a number greater than 2." << endl;
cin >> user_input;
} while (user_input<=2);

for (i=2; i < user_input; i++)
{
for (total=2; total< i; total++)
{cout <<"" << endl;
if (total%i!=0);
cout<<total%i << "Prime" << endl;

}
}

return 0;

}```

9. I had to have missed something in class, this assignment can't be this difficult.

10. > if (total%i!=0);
1. The ; at the end makes the if statement redundant.
2. Check which way round the trial division goes.

Perhaps it would be better with meaningful variable names
Code:
```for ( possiblePrime = 2 ; possiblePrime < n ; possiblePrime++ ) {
for ( trialDivisor = 2 ; trialDivisor < possiblePrime ; trialDivisor++ ) {
}
}```

11. Okay I swapped the locations of the two variables.

My cout statement still isn't giving me meaningful numbers though. I'm sure this is getting tiresome, but I appreciate all the help.

Code:
```// Alen
// Prime
//

# include <iostream>
using namespace std;
int main()
{

int user_input;
int i;
int n = 1;
int total;

do {
cout << "Enter a number greater than 2." << endl;
cin >> user_input;
} while (user_input<=2);

for (i=2; i < user_input; i++)
{
for (total=2; total< i; total++)
{cout <<"" << endl;
if (total%i!=0);
cout<<total%i << "Prime" << endl;

}
}

return 0;

}```

12. I think i got it. Thanks for all your help Salem, it is highly appreciated. Apparently bools are required for this. Would have saved me hours of trying.

Thanks for everything sir.