返回列表 發帖

質數 (一) - 判斷是否為質數

本帖最後由 陳品肇 於 2019-6-29 11:53 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     re:
  7.     int n,count=0; //count計算因數個數
  8.     cout<<"請輸入一個正整數:";
  9.     cin>>n;
  10.     for(int i=1;i<=n;i++)
  11.     {
  12.         if(n%i==0)
  13.         {
  14.            count++;
  15.         }
  16.     }
  17.    
  18.     if(count==2) //質數判斷
  19.     {
  20.         cout<<n<<"是質數"<<endl;
  21.     }else
  22.     {   
  23.         cout<<n<<"不是質數"<<endl;                        
  24.     }   
  25.     goto re;
  26.     system("pause");
  27.     return 0;   
  28. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int number,flag;
  7.     cout<<"Please enter a number:";
  8.     cin>>number;
  9.     for(int i=2;i<number;i++)
  10.     {
  11.         flag = (number%i == 0)?1:0;
  12.         if(flag == 1)
  13.             break;        
  14.         
  15.     }
  16.     if(flag == 1)
  17.         cout<<"不是質數喔";
  18.     else
  19.         if(number>1)
  20.             cout<<"這是質數";
  21.         else
  22.             cout<<"請勿亂輸入";
  23.     cout<<endl;
  24.     system("pause");
  25.     return 0;   
  26. }
複製代碼

TOP

本帖最後由 謝蓮金 於 2019-6-29 11:59 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b=0;
  7.     cout<<"請輸入一正整數:";   
  8.     cin>>a;
  9.     for(int i=1;i<=a;i++)
  10.     {
  11.        if(a%i==0)
  12.        {
  13.            b++;
  14.        }
  15.     }
  16.     if(b==2)
  17.     {
  18.     cout<<"是質數";
  19.     }else
  20.     {
  21.     cout<<"不是";     
  22.     }
  23.     system("pause");
  24.     return 0;
  25. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,b=0;
  7.     cout<<"請輸入一個正整數:";
  8.     cin>>a;
  9.     for(int i=1;i<=a;i++)
  10.     {
  11.       if(a%i==0)
  12.       {
  13.         b++;
  14.       
  15.       }
  16.     }
  17.     if(b==2)
  18.     {
  19.       cout<<"是質數";
  20.     }else
  21.     {
  22.     cout<<"不是質數";
  23.     }
  24.     cout<<""<<endl;
  25.     system("pause");
  26.     return 0;
  27. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    cout<<"***天才兒童的質數驗證器***"<<endl;
  7.    re:
  8.    int x,y=0;
  9.       cout<<"請輸入一個正整數:";
  10.       cin>>x;
  11.       for(int i=1;i<=x;i++)
  12.       {
  13.           if(x%i==0)
  14.           {
  15.               y++;
  16.           }
  17.       }
  18.       if(y==2)
  19.       {
  20.           cout<<x<<"是質數"<<endl;
  21.       }
  22.       else
  23.       {
  24.           cout<<x<<"非質數"<<endl;
  25.       }
  26.    goto re;
  27.    system("pause");
  28.    return 0;
  29. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.      int a;
  7.      
  8.      cout<<"請輸入第一整數";
  9.      cin>>a;
  10.      int sum=0;
  11.      for(int i=1; i<=a; i++)
  12.      {
  13.          if(a%i==0)
  14.          {
  15.             sum++;   
  16.          }         
  17.      }
  18.      if(sum==2)
  19.          cout<<a<<"是質數"<<endl;
  20.      else
  21.          cout<<a<<"不是質數"<<endl;
  22.   system("pause");
  23.   return 0;   
  24. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int a,count=0;
  7.     cout<<"請輸入一段整數:";
  8.     cin>>n;
  9.     for(int i=1;i<=n;i++)
  10.     {
  11.         if(n%i==0)
  12.         {
  13.            count++;
  14.         }
  15.     }
  16.    
  17.     if(count==2)  
  18.     {
  19.         cout<<n<<"是質數"<<endl;
  20.     }else
  21.     {   
  22.         cout<<n<<"不是質數"<<endl;                        
  23.     }   
  24.    
  25.    
  26.    
  27.    
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼

TOP

返回列表