TheAlgorithms-C/misc/Prime.c

23 lines
417 B
C
Raw Normal View History

2017-10-19 16:38:10 +08:00
#include <stdio.h>
2018-03-24 04:08:21 +08:00
#include <math.h>
2017-10-19 16:38:10 +08:00
int isPrime(int x) {
2018-03-24 04:08:21 +08:00
for (int i = 2; i < sqrt(x); i++) {
2017-10-19 16:38:10 +08:00
if (x%i == 0)
return 0;
}
return 1;
2018-03-24 04:08:21 +08:00
2017-10-19 16:38:10 +08:00
}
int main() {
int a;
printf("Input a number to see if it is a prime number:\n");
scanf("%d", &a);
if (isPrime(a))
printf("%d is a prime number.\n", a);
else
printf("%d is not a prime number.\n", a);
return 0;
}