有那位高手会帮看看!
C语言要完成的小程序判断一个数字是否是素数。
要能运行的程序!!!!
#include <stdio.h> int isprime (unsigned n) { if (n == 0 || n == 1) printf ("%d isn't a prime!\n", n); else { unsigned index; for (index = 2; index < n; index ++) if ((n % index) == 0) break; if (index == n) printf ("%d is a prime!\n", n); else printf ("%d isn't a prime!\n", n); } return 0; } int main (void) { unsigned num; printf ("Please enter a natural number(q to quit):"); while (scanf ("%d", &num) == 1) { isprime (num); printf ("Please enter another natural number(q to quit):"); } return 0; }
#include<stdio.h> #include<math.h> void main() { int number, i, flag; //用flag作为标志来判断输入数字是否为素数,当flag=1时是素数,当flag=0则不是素数 printf("\nPlease imput a number(range(number>=2)):\n"); //素数必须大于二,小于二的数判断无意义 scanf("%d",&number); if(number<=1) printf("\n****Imput error!****\n"); for(i=2; i<=sqrt(number); i++) { if(number % i == 0) //能被整除的数不是素数 flag=0; } if(flag != 0) printf("\n************%d是一个素数*************\n",number); else printf("\n************%d不是一个素数****************\n",number); getchar(); }