質數定義為除了自己和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);
        }
    }
}
arrow
arrow
    文章標籤
    c 質數
    全站熱搜

    Will 發表在 痞客邦 留言(0) 人氣()