#include #include #include #include using namespace std; //define function prototypes void printPrimes(int); //int getBound(); bool numGood(string); bool isPrime(int); int main() { int upBound; string usIn; bool inGood = false; //loop through until good input while(inGood == false) { //ask for upper bound cout << "Enter the upper bound of prime numbers you want printed: "; cin >> usIn; //check to see if num is good inGood = numGood(usIn); } //convert string to int upBound = atoi(usIn.c_str()); //print prime numbers up to that point printPrimes(upBound); return 0; } //check if user input is a good positive integer //precondition:user has inputed a string for the number specified //postcondition: the number is a valid floating point number or is not //return:true if number is good, false if number is not good bool numGood(string input) { for(int x = 0; x < input.size(); x++) { if(input[x] < 48 || input[x] > 57) { cout << "Bad input, enter a new guess" << endl; return false; } } return true; } //check if number passed is prime and return t/f //precondition: the program has a number that it wants to know if it is prime or not //postcondition: the number either is or is not a prime number //return: true if number is prime, false if not bool isPrime(int number) { for(int i = 2; i < number/2 +1; ++i) { if(number%i == 0) { /* if divisible */ return false; } } /* if number is not divisible by anything * than it must be prime */ return true; } //print all the numbers up to given bound that are prime //precondition: User has told up to what number they want the primes printed //postcondition: All prime numbers up to user specified have been printed to the screen //return: nothing. void printPrimes(int bound) { //loop through until upper bound for(int j = 3; j <= bound; j++) { //check if current increment is prime number and print if so if(isPrime(j) == true) { cout << j << " is a prime number." << endl; } } }