A simple C library for working with prime numbers.
Checks if a number is prime.
- Returns:
1if the number is prime0if the number is not prime-1if the number is less than 2
Finds the next prime number starting from the given number.
- Returns: The next prime number greater than or equal to
x
prime.h- Header file with function declarationsprime.c- Implementation of prime number functions
Include the header file in your C program:
#include "prime.h"
int main() {
int num = 10;
if (is_prime(num) == 1) {
printf("%d is prime\n", num);
} else {
printf("%d is not prime\n", num);
}
printf("Next prime after %d is %d\n", num, next_prime(num));
return 0;
}Compile your program with:
gcc -o program main.c prime.c -lmNote: The -lm flag is needed to link the math library for the sqrt() function.