第9.1.2 小节代码 判定素数
//This is a comment
//Author:King
//Time:2020/12/6
//Reference:C Programming:A Modern Approach,Second Edition
/***************************************************************
9.1.2 小节代码 判定素数(质数)
****************************************************************/
#include <stdio.h>
#include <stdbool.h> // C99 only
//#include <time.h>
//#include <stdlib.h>
bool is_prime(int n)
{
int div;
if(n <= 1)
printf("Please enter number bigger than 1\n");
printf("Enter a number:");
scanf("%d",&n);
for(div = 2; div * div <= n; div++)
{
if(n % div == 0)
return false;
}
return true;
}
int main(void)
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(is_prime(n))
printf("Prime\n");
else
printf("Not prime\n");
system("pause"); //加入该函数后可以使得产生的exe单独运行,不会发生闪退。也可以加入其它函数使得main函数无法返回即可。如while(1)、getchar() 等
return 0;
}