/* FILE: isPrime-v2.cpp tests n potential primes entered by user, using an EOF-controlled loop employs user-defined function, isPrime() */ #include using namespace std; //prototypes bool isPrime(unsigned long n); int main(){ int n, //input integer pd; //potential divisor //prompt and read n cout << "enter n > 1 (c-d to exit): "; cin >> n; //driver loop while(cin){ //display result cout << endl << n << ((isPrime(n)) ? " is prime " : " is composite" ); //prompt and read n cout << endl << "enter n (c-d to exit): "; cin >> n; } //quit cout << endl << "Exiting..." << endl; } /* isPrime()------------------------------------------------------------------ * simple, brute-force predicate for prime numbers * returns true if n is prime; otherwise returns false */ bool isPrime(unsigned long n){ unsigned long pd = 2; //potential divisor //generate and test divisors while(n % pd != 0) pd++; //return result return (n == pd); }