公倍數與公約數

#include<stdio.h>
#include <iostream>
#include<math.h>

using namespace std;

//公約數
int getCommoDivisor(int x, int y)
{
    int k;
    if (x < y)
    {
        k = x;
        x = y;
        y = k;
    }
    if (x%y == 0)
        return abs(y);
    else
        return getCommoDivisor(y, x%y);
}

//公倍數
int getCommonMultiple(int x, int y)
{
    int z = getCommoDivisor(x, y);
    int t = (x*y) / z;
    return t;
}

int _tmain(int argc, _TCHAR* argv[])
{
    do
    {
        int x, y, z, t;
        cout << "請輸入x=" << endl;
        cin >> x;
        cout << "請輸入y=" << endl;
        cin >> y;
        z = getCommoDivisor(x, y);
        t = getCommonMultiple(x, y);
        cout << x << "" << y << "的最小公倍數爲:" << t << endl;
        cout << x << "" << y << "的最大公約數爲:" << z << endl;

        //getchar();

        system("pause");

        system("cls");
        
    } while (1);

    return 0;
}
相關文章
相關標籤/搜索