Created
December 15, 2019 22:42
-
-
Save thinkphp/e3aba26727f716a98cb2a76d730a7175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Test if a number is Prime or not. | |
*/ | |
#include <stdio.h> | |
int isPrime(int n) { | |
if(n == 0 || n == 1) return 0; | |
if(n == 2 || n == 3) return 1; | |
int prime = 1, k = 2; | |
while(prime && k * k <= n) { | |
prime = (n % k != 0); | |
k++; | |
} | |
return prime; | |
}; | |
int main() { | |
int n; | |
printf("n="); | |
scanf("%d", &n); | |
if(isPrime(n)) printf("%d is Prime Number!\n", n); | |
else printf("Is not prime!\n"); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment