Prime numbers problems getting the propper code down

As the title says, i'm trying to write a program that will print out the all prime numbers below 100..

What i've been trying to write in code is:

Check if any of the numbers less than a is divisible with a. if yes than it's not a prime, if none of the numbers less than a is divisible by a it's a prime

Would this be the correct way of doing it?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include using namespace std; int Search = 1, a = 2; int main() { while(a < 100) { for (a; Search < a; Search++) { if(a % Search == 0) { Search = 1; break; // not prime } } if(a % Search != 0) // if a % Search did not = 0 during the whole for loop than it's a prime { int prime = a; Search = 1; cout << prime << endl; } a++; } system("pause"); return 0; }``````

Last edited on
Lots of whitespace, might want to cut down on that a little.

If it doesn't work then it isn't correct :] but you aren't too far off, if Search is 1, then a % 1 = 0.

what is a prime number?
Well, it's a number that is only divisible by 1 and itself.

that's why i'm first having an if statement that checks the for loop if "a" is divisible by any value that "search" obtains if it is not then it goes on then it breaks out of the foor loop and adds a value to "prime"

 if Search is 1, then a % 1 = 0.

oh so i have to set search = 2 as start value?

 ``1234567891011121314151617181920212223242526272829`` ``````int Search = 2, a = 2; int main() { while(a < 100) { for (a; Search < a; Search++) { if(a % Search == 0) { Search = 2; break; // not prime } } if(a % Search != 0) // if search did not = 0 during the whole for loop than it's a prime { int prime = a; Search = 2; cout << prime << endl; } a++; } system("pause"); return 0; }``````

would look something like this?
Last edited on
Why don't you run it and see?

now, imagine Search HAD to be 1, you can't change what value it starts as, find some other way to make sure it doesn't check "a % Search == 0" when Search == 1
Last edited on
well, i could just add this:

 `` `` ``if(a % Search == 0 && Search != 1)``

But program is still not printing anything
run both of these versions, note I added a cout so you can see the value of a and the value of search, and what the value of a % Search is.

So why does it not detect that the number is prime?

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int Search = 1, a = 2; int main() { while (a < 100) { for (; Search < a; Search++) { if (a % Search == 0) { Search = 1; break; } } cout << a << " % " << Search << " = " << a % Search << endl; if (a % Search != 0) { int prime = a; Search = 1; cout << prime << endl; } a++; } return 0; }``````

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int Search = 1, a = 2; int main() { while (a < 100) { for (; Search < a; Search++) { if (Search != 1 && a % Search == 0) { Search = 1; break; } } cout << a << " % " << Search << " = " << a % Search << endl; if (a % Search != 0) { int prime = a; Search = 1; cout << prime << endl; } a++; } return 0; }``````

what if you moved Search != 1 to the other if statement? what if you had them in both if statements?

experiment a little bit, see what you come up with, what if you assumed it was a prime unless you had proof it wasn't? or vice-versa? how would you store something like that in a variable, what type would you use?
Last edited on
here we go :D

seems like the a-1 was missing :D
 `` `` ``for (a; Search < a-1; Search++)``

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int Search = 1, a = 2; int main() { while (a < 100) { for (a; Search < a-1; Search++) { if (a % Search == 0 && Search != 1) { Search = 1; break; } } if (a % Search != 0) { int prime = a; Search = 1; cout << prime << endl; } a++; } system("pause"); return 0; }``````

Awesome :]
Topic archived. No new replies allowed.