猿问

用函数判断一个数是不是素数

用函数判断一个数是否是素数,要求主函数负责输入和输出,子函数负责其他过程
踏月光
浏览 1767回答 3
3回答

望远

#include<stdio.h> #include<math.h> int isPrime(int n) { if(n==1) return 0; for(int i=2;i<=sqrt(n);i++) { if(n%i==0) return 0; } return 1; } void main() { int n,flag; printf("请输入一个正整数:"); scanf("%d",&n); flag=isPrime(n); if(flag) printf("%d是素数!\n",n); else printf("%d不是素数!\n",n); }

_Jack_Han_

#include <stdio.h> static int judge(int n){     int result;     int i;     for(i=2;i<n;i++){         if(n%i==0){           result=0;           break;         }else{           result=1;         }     }     return result; } int main(){   int num=19;   int result=judge(num);   if(result==0){     printf("%d不是素数\n",num);   }else if(result==1){     printf("%d是素数",num);   }   return 0; }

慕的地8582982

WWWWHDBNWKRLRHPWWURAYSRWZJAEYVOSSMCIGXTJEHQTIFSSIDCXOBSSIUMTNVGQYYEATJGGHLEHUHNAOJTCGFJRYMLZQZPJWZGHYPJRIXSVHKXQWGNTNQQYYUJWMJXNXGXUHEBENDCPZAKZZQSWMTEXHOIHKUKVLOYZGKANGDNWTNSDGCMMCQGDQWJQGWEASBYBBISOHFCZBSCPGXQWWDPZWQALAQTLRCBGJKIHXKLWZTGPBRCSVCQNXRKORMSMWMDDHGXNRQDDRYRFEMDJDWAHNNXQUUUFRVYMWGPXREMJAXHUERCMNHVLIFFPIDQANNRRAHPSISUNKSGEYXREFGOFVCSJNRZYQWJTZKXEEVPFJNXUKKCVPDWLSBFTMBFCMVBRLLIMMMQJXQBUBPMGDGESXUDREEPCZWXVILMFTGZQUFLPSLVVVPZGLYRRPKYDDKYKEENKOOPYVPIWZNXPYFIWWUGTJZTAHAIZCWWPMTNHBFMOKBFVFPBCALXHQUFFOAJTXMCYSYWMZCDIOUXQQUDLOICBXUUEUBYSCJLBHALYPCVDAUYXATZBAWCFPSLZJDZQNUPMGWFCATQLMDPTCFVVYOEXEUAEUDNXUUDKHNZXGWZWLLLWSVYOVYPTJRYEDTBUEPLCJJMIICLHRJYFWYVOPZYSRCSQMISJZZDMMQIWIWZPSCIMWAOCFVYRLUKXOARIRDJGMZGLGGTPEEANUBADDWWWGWVSTKQPCLCLRAHOCIYIBLHYAXUQVIVMHNGZGNGVLIBRUYOSLAWDMFZYCFLSYKDPGQITSSMVPVOHHHTLPZEBKNPZFBBFMINNKXLNWFNACIFEYHWCRLSHHTJQZCRQYLHRJMMNMJCPTTGCWGSSCYLT
随时随地看视频慕课网APP
我要回答