當前位置:菜譜大全網 - 減脂餐食譜 - 用C語言寫程序求任意兩個數的最大公約數和最小公倍數!在線等待答案...

用C語言寫程序求任意兩個數的最大公約數和最小公倍數!在線等待答案...

# include & ltiostream & gt

# include & ltcmath & gt

使用命名空間std

int Gyshu(int,int);

int Gbshu(int,int);

int main(){

int a,b;

cout & lt& lt"請輸入第壹個數字:"

CIN & gt;& gta;

cout & lt& lt"請輸入第二個數字:"

CIN & gt;& gtb;

?int c=Gyshu(a,b);

?int d=Gbshu(a,b);

cout & lt& lt"最大公約數是:"

cout & lt& lt"最小公倍數是:"

?返回0;

}

//最大公約數的函數

int Gyshu(int a,int b){

int c = 1;

如果(a & gt=b){

?for(int I = 1;我& lt= b;i++){

if(a % I = = 0 & amp;& ampb % I = = 0)c = I;

?}

}

否則{

?for(int I = 1;我& lt= a;i++){

if(a % I = = 0 & amp;& ampb % I = = 0)c = I;

?}

}

?返回c;

}

//最小公倍數的函數

int Gbshu(int a,int b){

int c = 1;

如果(a & gt=b){

for(int I = 1;我& lt= b;i++){

?if(a % I = = 0 & amp;& ampb % I = = 0)c = I;

?}

}

?否則{

for(int I = 1;我& lt= a;i++){

?if(a % I = = 0 & amp;& ampb % I = = 0)c = I;

}

}

c = a * b/c;

?返回c;

}

以下是執行效果圖: