I am trying to find the amount of prime numbers between 1-100

Here is my code, for some reason I am getting 51 when the answer is 25. The reason I am using a user made function is because I am going to be expanding the code to 100,000 with segments of 100. Is it my math that is wrong?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

using namespace std;

int isPrime(int number);


int main()
{
	int i = 1, amountPrime = 0;
	bool result;
	while( i < 100)
	{	
		
		result = isPrime(i);
		
		if(result == true)
			amountPrime++;
		else;
		
		i++;
	
	}
	
	cout << amountPrime << endl;
	



	system("PAUSE");
	return 0;
}

//isPrime function

int isPrime(int number)
{
	
	int c;
	bool isPrime = true;
	c = number;

	for(int i = 2; i < c; i++)
	{
		if (c % i == 0)
			isPrime = false;
			break;

	}

	if(isPrime)
		return true;
	else
		return false;

	

}
Well, for one, replace the last few lines of the isprime function with:

return isPrime

Also, make isPrime return a boolean value, not an integer value.

As for your actual issue, look at the for loop in isPrime(). Specifically, look at the if statement... are you missing something there?
Topic archived. No new replies allowed.