Solved

# C++ prime counter within range

Posted on 2005-02-24

Great site!

I have written a c++ program that is supposed to count prime numbers in a range entered by the user but I think my prime tester function is not working. Here is what I have:

#include <iostream>

using namespace std;

bool isPrime (int n);

int primeCount (int x, int y);

int main ()

{

int x, y;

cout << "Enter a range : " << endl;

cin >> x >> y;

cout << "The number of primes between " << x << " and " << y << "is " << primeCount (x, y) << endl;

return 0;

}

bool isPrime (int n)

{

int i;

for (i=2; i<=n;i++)

{

if ((n % i) == 0)

return false;

return true;

}

}

int primeCount (int x, int y)

{

int a=0;

for (x=x;x<=y;x++)

{

if (isPrime(x))

a++;

return a;

}

}

I am very frustrated because I have tried everything. Any help would be appreciated :)!