// Program: prime.cpp // Test if a given natural number is prime. #include #include int main () { // Input unsigned int n; std::cout << "Test if n>1 is prime for n =? "; std::cin >> n; assert (n > 1); // Computation: test possible divisors d unsigned int d; for (d = 2; n % d != 0; ++d); // Output if (d < n) // d is a divisor of n in {2,...,n-1} std::cout << n << " = " << d << " * " << n / d << ".\n"; else { // no proper divisor found assert (d == n); std::cout << n << " is prime.\n"; } return 0; }