質數定義為除了自己和1之外不能被任何數整除
用了布林變數isPrime來判斷, 若迴圈執行過程中
跑到isPrime為true時印出此質數
#import <Foundation/Foundation.h>
int main(void)
{
bool isPrime;
int num1;
scanf("%d", &num1);
for (int p = 2; p <= num1; ++p)
{
isPrime = true;
for (int d = 2; d < p; ++d)
{
if (p % d == 0)
{
isPrime = false;
}
}
if (isPrime)
{
printf("%d\n", p);
}
}
}
文章標籤
全站熱搜
留言列表