// 给出一个大于或等于3的正整数,判断它是不是一个素数 #include <stdio.h> #include <math.h> int sushu(int x) { int i; if( x >= 3 ) { for( i = 2; i <= sqrt(x); i++ ) { if( x % i == 0 ) return 1; } return -1; } else printf("输入的数太小啦,请重新输入\n"); } int main() { int a; int b; printf("请输入一个大于3的数:"); scanf("%d",&a); b = sushu(a); if( b == 1 ) printf("%d不是素数\n",a); else printf("%d是素数\n",a); return 0; } <img src="http://img.blog.csdn.net/20150423114622109?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> <img src="http://img.blog.csdn.net/20150423114512417?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb3lhcWlhbjU1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
时间: 2024-10-09 06:59:19