- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int number,number2,x=1,cont=0;
- cout<<"請輸入一個數字:";
- cin>>number;
- cout<<"請輸入另一個數字:";
- cin>>number2;
- cout<<number<<"和"<<number2<<"的公因數有:";
- while(x<=number && x<=number2)
- {
- if(number%x == 0 && number2%x == 0)
- {
- cout<<x<<" ";
- cont+=1;
- }
- x+=1;
- }
- cout<<endl;
- cout<<"共"<<cont<<"個"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |