#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
int n;
bool result;
cout << "Enter a positive integer: ";
cin >> n;
result = isPrime(n);
if (result) {
cout << n << " is a prime number";
} else {
cout << n << " is not a prime number";
}
return 0;
}
Enter a positive integer: 31
31 is a prime number
isPrime()
that takes an integer argument and returns a boolean value indicating whether or not the number is prime. main()
function, the program prompts the user to enter a positive integer, calls the isPrime()
function on that integer, and outputs the result.