- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int x, y, smaller, maxf;
- cout<<"請輸入第一個數: ";
- cin>>x;
- cout<<"請輸入第二個數: ";
- cin>>y;
- smaller=(x<y)?x:y;
- cout<<x<<"與"<<y<<"的公因數有: ";
- for(int i=smaller; i>=1; i--)
- {
- if(x%i==0 && y%i==0)
- {
- maxf=i;
- break;
- }
- }
- cout<<x<<"與"<<y<<"的最大公因數是:"<<maxf<<endl;
- system("pause");
- return 0;
- }
複製代碼 |