標(biāo)題: C++語言判斷素數(shù)程序 [打印本頁]

作者: daming    時間: 2014-12-30 02:03
標(biāo)題: C++語言判斷素數(shù)程序
本帖最后由 daming 于 2014-12-30 02:16 編輯
  1. #include<iostream.h>
  2. #include<math.h>
  3. int fc(int n)
  4. {
  5. int flag=0;
  6. int i;
  7. for(i=2;i<=sqrt(n);i++)
  8.   if(n%i==0)
  9.    flag=1;
  10. return flag;
  11. }
  12. void main()
  13. {
  14. int n;
  15. cout<<"input data:\n";
  16. cin>>n;
  17. if(fc(n)==0)
  18.   cout<<n<<"是素數(shù)\n";
  19. else
  20.   cout<<n<<"是合數(shù)\n";
  21. }
復(fù)制代碼







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1