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.

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
#include <iostream>
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<<number<<" is prime\n";
			}

		}
	number--;
	}while (number>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

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
#include <iostream>
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<<number<<" is prime\n";
	}
	
	number--;
	}while (number>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

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
#include <iostream>
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<<number<<"     is prime        ";
		

		cout<<number<<endl;//to see if the number being checked is changing
	}while (number>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:

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
#include <iostream>
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<<number<<" is prime";
				
			}
	}while (number>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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    do
    {
        primecheck = true;
        for (int checker=2; checker<number; checker++)
        {
            if (number % checker == 0)
            {
                primecheck = false;
                break;
            }
        }
        if (primecheck)
            cout << number << "     is prime        ";

        number--;
    } while (number>1);
Last edited on
for (int checker=2; checker<=number; checker++)
Notice anything that would cause problems here?
Thanks for your help!
Topic archived. No new replies allowed.