您的位置:首页 > 其它

编写函数,分别求两个整数的最大公约数和最小公倍数.

2017-12-06 20:53 756 查看
#include<iostream>

using namespace std;

int fun1(int a,int b)

{
int x,y,t;
if(a>b)
{
x=a;
   y=b;
}
else
x=b;
   y=a;
while(x%y!=0)
{
x=y;
t=x%y;
y=t;
}
return y;

}

int fun2(int a,int b)

{
int t;
if(a>b)
t=a;
else t=b;
while(t%a!=t%b)
{
t++;
}
return t;

}
int main()
{
int a,b;
cout<<"请输入两个整数"<<endl;
while(1<2)
{
cin>>a>>b;

cout<<"最大公约数为"<<fun1(a,b)<<endl;
cout<<"最小公倍数为"<<fun2(a,b)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数例题
相关文章推荐