本帖最後由 陳品肇 於 2018-12-15 11:53 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- int x,count=0;
- cout<<"請輸入一個正數: ";
- cin>>x;
-
- for(int i=1;i<=x;i++)
- {
- if(x%i==0)
- {
- count++; //算出正數因數有幾個
- }
- }
-
- if(count==2) //判斷是否為質數
- {
- cout<<x<<"是質數!"<<endl;
- }else
- {
- cout<<x<<"不是質數!"<<endl;
- }
- goto re;
- system("pause");
- return 0;
- }
複製代碼 |