this solution should return 1 (i.e false) if n is prime, otherwise it returns 0,
the problem is that it always returns 1 because of the first if clause (I've specified a number in the debugging properties as usual)
if (argc != 2) {
return 1;
}
#include <stdio.h> // for ptrintf function
#include <math.h> // for square root function
#include <stdlib.h> // for strtol function
int is_prime(int n, int i) {
if (i == sqrt(n)) {
return 0;
}
if (n % i == 0) {
return 1;
}
return is_prime(n, i + 1);
/* for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 1;
}
}
return 0; */
}
int main(int argc, char* argv[]) {
if (argc != 2) {
return 1;
}
int n = strtol(argv[1], NULL, 10);
if (n < 0) {
return 1;
}
int boolean_n = is_prime(n, 2);
return 0;
}