#include <iostream>
#include <cmath>
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
int main() {
while (true) {
int n;
std::cout << "Enter a number (or a negative number to exit): ";
std::cin >> n;
if (n < 0) {
std::cout << "Exiting...\n";
break;
}
if (isPrime(n)) {
std::cout << n << " is a prime number.\n";
} else {
std::cout << n << " is not a prime number.\n";
}
std::cout << '\n';
}
return 0;
}
No comments:
Post a Comment