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