### troubleshoot prime number calculator

Hi i've searched through some articles and i think the mathematical part of my code is correct, but it doesnt run as expected. many thanks in advanced.

edit: the program is required to calculate all the primes between 2 and 2000.

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; void prime() { bool primecheck = true; int number=2000; do { for (int checker=2; checker<=number; checker++) { if (number % checker == 0) { primecheck = false; } if (primecheck) { cout<1); } int main() { prime(); system("pause"); return 0; }``````
Last edited on
Can you explain what you mean by "it doesn't run as expected"?

Edit: Why are lines 17-20 inside the loop??
Last edited on
sorry i should have been more clear. it doesnt display anything, ill take those lines outside the loop. thanks

edit:this is my new code, still nothing is displayed. just asks me to press a key and exits, as if primecheck is never true

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include using namespace std; void prime() { bool primecheck = true; int number=2000; do { for (int checker=2; checker<=number; checker++) { if (number % checker == 0) { primecheck = false; } } if (primecheck == true) { cout<1); } int main() { prime(); system("pause"); return 0; }``````
Last edited on
Instead of \n, try <<std::endl - I think it may be outputting but just not flushing to the display.
still nothing, thanks thogh
Weird, I don't see any reason why this wouldn't output. Try throwing a random cout statement at the very beginning of the function and see if that works. If that doesn't, then try putting one in main.
so i put in some random cout statements in as follows, and i get an output at least, but it just counts down from 2000

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include using namespace std; void prime() { cout<<"prime function running"; bool primecheck = true; int number=2000; do { for (int checker=2; checker<=number; checker++) { if (number % checker == 0) { primecheck = false; number--;//added this as once if found if 'number' is not prime we can move on the the next break; } } if (primecheck) cout<1); } int main() { cout<<"main running"; prime(); system("pause"); return 0; }``````
Ok so output does work. Is it outputting at line 26? Sounds like a logic error now.
sorry i should have mentioned yes it does output at line 26 ( and 37 and 6 as expected) and 'number' is counting down within the if statement (line18)
Sorry, I actually meant at line 23. But since you didn't mention it in the list of lines that does get outputted, I can assume line 23 is never hit.

primecheck is never set back to true when testing a new number, so it's going to be remain false after the first composite number is hit, which how you have this set up will be the very first number (2000). Since primecheck is never true, that statement will never get hit.
bearing in mind what you've said, here is my code:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include using namespace std; void prime() { cout<<"prime function running"; bool primecheck; int number=2000; do { primecheck = true;//reseting prime check for every new number for (int checker=2; checker<=number; checker++)//check against all the integers less than number { if (number%checker==0)//if the remainder of the division is zero i.e. it does divide then.... { primecheck = false;//...its not prime number--;//added this as once if found if 'number' is not prime we can move on the the next break; } } if (primecheck == true)//!!**!!**this is never true**!!**!!**!! { cout<1); } int main() { cout<<"main running"; prime(); system("pause"); return 0; }``````

but line 25 is still not executed
Several errors.
The for-loop should have `checker<number`, not `<=`.
The boolean `primecheck` needs to be reset for each new number.
The `number--` needs to be executed regardless of whether or not the number is prime.
 ``12345678910111213141516`` `````` do { primecheck = true; for (int checker=2; checker1);``````
Last edited on
`for (int checker=2; checker<=number; checker++)`
Notice anything that would cause problems here?
Topic archived. No new replies allowed.