Would you like to react to this message? Create an account in a few clicks or log in to continue.


您沒有登錄。 請登錄註冊

【C語言】輸入任一整數,找質數

向下  內容 [第1頁(共1頁)]

1【C語言】輸入任一整數,找質數 Empty 【C語言】輸入任一整數,找質數 周三 4月 13, 2011 9:21 pm

Williamd

Williamd
Admin

代碼:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int a,b,c;
  int d=0;
 
  printf("Input a integer to set a range\n");
  scanf("%d",&a);
  printf("1~%d的質數有:",a);
 
  for(b=1;b<=a;b++){
                  int t=0;
                  for(c=1;c<=b;c++){
                  if(b%c==0){
                              t++;
                              }                 
                                    }
                  if(t==2){
                            d++;
                            printf("%d ",b);
                            }                 
                    }
                    printf("\n總共有%d個質數\n",d); 
 
  system("PAUSE");   
  return 0;
}

http://c-wind.luntanlong.com

回頂端  內容 [第1頁(共1頁)]

這個論壇的權限:
無法 在這個版面回復文章