1 回答

TA貢獻71條經(jīng)驗 獲得超14個贊
找了一個 懶得編寫
#include <stdio.h>
bool isPrimeNum(int x)
{
? ? if (x == 1)
? ? ? ? return false;
? ? else if (x <= 0)
? ? ? ? return false;
? ? else if (x == 2)
? ? ? ? return true;
? ? else
? ? {
? ? ? ? for (int i = 2; i < x; i++)
? ? ? ? {
? ? ? ? ? ? if (x%i == 0)
? ? ? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? return true;
? ? }
}
int main(void)
{
? ? int x;
? ? char ch;
? ? do{
? ? printf("請輸入一個大于1的自然數(shù):\n");
? ? scanf("%d", &x);
? ? if (isPrimeNum(x) == false)
? ? ? ? printf("%d不是素數(shù)\n",x);
? ? else if (isPrimeNum(x) == true)
? ? ? ? printf("%d是素數(shù)\n",x);
? ? printf("是否繼續(xù)(y/n):\n");
? ? scanf("%c", &ch);
? ? }while ('y'== ch || 'Y' == ch);
? ? return 0;
}
- 1 回答
- 0 關(guān)注
- 1330 瀏覽
添加回答
舉報