本帖最後由 enter 於 2015-1-15 21:42 編輯
#include <stdio.h>
int main(void)
{
int x,i,factor;;
printf("enter a number:\n");
scanf("%d",&x);
factor=0;
for(i=2;i<=x/2;i++) //這裡如果只設成i<x/2,那麼假設x=38,i<(38/2)是為18,則後面i++變成19時,就不能再進入運算迴圈。i++是指第二條件符合之後才運算的。
if(!(x%i))
{
printf("%d is a factor of %d.\n",i,x);
factor++;
}
if(factor==0) printf("%d is a prime.\n",x);
printf("There are %d factors for %d.\n",factor,x);
return 0;
} |