Here’s a recursive primality test function:
int test_prime(unsigned n, unsigned d)
{
if (n < 2) {
return 0;
}
if (d == 1) {
return 1;
} else {
if (n % d == 0) {
return 0;
} else {
return test_prime(n, d - 1);
}
}
}
Use it like:
unsigned n;
std::cin >> n;
std::cout << (test_prime(n, n / 2) ? "prime" : "not prime") << "\n";
solved code showing prime number (c++) using recursive function and without using any loop [closed]