TheAlgorithms-C/misc/Prime.c

32 lines
566 B
C
Raw Normal View History

2017-10-19 11:38:10 +03:00
#include <stdio.h>
2018-03-23 23:08:21 +03:00
#include <math.h>
2017-10-19 11:38:10 +03:00
int isPrime(int x) {
if (x == 2) {
return 1;
}
if (x < 2 || x % 2 == 0) {
return 0;
}
double squareRoot = sqrt(x);
for (int i = 3; i <= squareRoot; i += 2) {
if (x % i == 0)
2017-10-19 11:38:10 +03:00
return 0;
}
return 1;
2018-03-23 23:08:21 +03:00
2017-10-19 11:38:10 +03: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;
}