/* primes.cpp * This program will generate primes up to and possibly including * a user specified number. The program will force the user to enter * a number between 0 and 100,000. */ /* A number N is prime if the only divisors of N are 1 and N. EX.. 2, 3, 17, 41 * */ #include #include using namespace std; int main() { int upperBound; // force the user to enter a positive integer less than 100000 do { cout << "Enter a positive integer less than 100,000" << endl; cin >> upperBound; }while (upperBound <= 0 || upperBound >= 100000); cout << "The primes less than " << upperBound <<" are: " << endl; // The first for loop generates the candidates. // The second for loop generates the integers used as divisors. int primeCandidate,possibleDivisor; for (primeCandidate = 2; primeCandidate <= upperBound; primeCandidate++ ) { for (possibleDivisor = 2; possibleDivisor < primeCandidate; possibleDivisor++ ) { // if possibleDivisor divides the primeCandidate, then the remainder // upon division of ( primeCandidate / possibleDivisor ) is zero // Hence possibleCandidate is not prime, break out of the inner loop // then pick the next candidate. if (0 == (primeCandidate % possibleDivisor)){ break; } } if (primeCandidate == possibleDivisor){ cout << possibleDivisor << endl; } } system("pause"); return 0; }