mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
Check if a number is prime.
This commit is contained in:
parent
98005e3fde
commit
4124e8c9d8
25
misc/Prime.c
Normal file
25
misc/Prime.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int isPrime(int x) {
|
||||||
|
if (x == 1)
|
||||||
|
return 0;
|
||||||
|
if (x % 2 == 0)
|
||||||
|
return 0;
|
||||||
|
for (int i = 3; i < x; i++) { // May replace x with sqrt(x)
|
||||||
|
if (x%i == 0)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user