返回列表 發帖

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

本帖最後由 鄭繼威 於 2023-3-24 19:55 編輯


可以直接拿有哪些因數 (一)去改,主要就是if裡面多加了計數器,最後判斷計數器是不是=2
1.輸入數字到x
2.for迴圈 1~x
2-1.if整除的話計數器+1
3.判斷計數器是不是=2
4.輸出結果
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     cout<<"***質數驗證機***"<<endl<<endl;
  7.     re:
  8.     int x,counter=0;        //計數器
  9.     cout<<"輸入一正整數: ";
  10.     cin>>x;

  11.     //for迴圈
  12.     //1~x
  13.     for(int i=1; i<=x; i++)
  14.     {
  15.          //判斷有沒有整除(餘數為0代表整除)
  16.          if(x%i==0)
  17.          {
  18.              //整除的話 +1
  19.              counter++;
  20.          }
  21.     }
  22.     if(counter==2)
  23.     {
  24.         cout<<x<<"是個質數!"<<endl;
  25.     }
  26.     else
  27.     {
  28.         cout<<x<<"不是質數!"<<endl;
  29.     }
  30.     goto re;
  31.     return 0;   
  32. }
複製代碼

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     cout<<"*** 質數驗證機 ***"<<endl<<endl;
  7.     re:
  8.     int x,counter=0;        
  9.     cout<<"輸入一正整數:";
  10.     cin>>x;

  11.     for(int i=1; i<=x; i++)
  12.     {
  13.          if(x%i==0)
  14.          {
  15.              counter++;
  16.             
  17.          }
  18.     }
  19.     if(counter==2)
  20.     {
  21.         cout<<x<<"是質數!"<<endl;
  22.     }
  23.     else
  24.     {
  25.         cout<<x<<"不是質數..."<<endl;
  26.     }
  27.     goto re;
  28.     return 0;   
  29. }
複製代碼

TOP

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main(){
  5.     cout<<"輸入一個數: "<<endl;
  6.     int x,counter;
  7.         cin>>x;
  8.         
  9.         
  10.         cout<<x<<"的因數:";
  11.         for(int i=1;i<=x;i++){
  12.                
  13.                 if(x%i==0){
  14.                         
  15.                         cout<<i<<" ";
  16.                         counter++;
  17.            }
  18.    
  19.        }
  20.     cout<<endl;
  21.     if(counter==2)
  22.     cout<<x<<"是質數"<<endl;
  23.    
  24.    else
  25.    
  26.     cout<<x<<"不是質數"<<endl;

  27.        system("pause");
  28.         return 0;
  29. }   
複製代碼

TOP

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

TOP

本帖最後由 何權晉 於 2023-3-24 20:16 編輯
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     re:
  6.     int x, counter=0;
  7.     cout<<"Enter a random whole number: ";
  8.     cin>>x;
  9.     for(int i=1;i<=x;i++)
  10.     {
  11.             if(x%i==0)
  12.             {
  13.                     counter++;
  14.                     }
  15.                     }
  16.     if(counter==2)
  17.     {
  18.                   cout<<"Prime number"<<endl;
  19.                   }
  20.     else
  21.     {
  22.         cout<<"Not a prime number"<<endl;
  23.         }
  24.     cout<<endl;
  25.       goto re;
  26.         return 0;
  27. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;

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

TOP

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

TOP

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

TOP

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

TOP

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

TOP

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

TOP

返回列表