本帖最後由 洪藜芸 於 2019-6-15 11:18 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int a,b,d=0;
- cout<<"請輸入第一個數:";
- cin>>a;
- cout<<"請輸入第二個數:";
- cin>>b;
- cout<<a<<"和"<<b<<"的公因數有:";
- if(a>b)
- {
- for(int c=1;c<=b;c++)
- {
- if(a%c==0 && b%c==0)
- {
- cout<<c<<" ";
- d=d+1;
- }
-
- }
- }else if(a<b)
- {
- for(int c=1;c<=a;c++)
- {
- if(a%c==0 && b%c==0)
- {
- cout<<c<<" ";
- d=d+1;
- }
- }
-
- }
- cout<<endl;
- cout<<"共有"<<d<<"個!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |