编写函数,分别求两个整数的最大公约数和最小公倍数.
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;
}
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;
}
相关文章推荐
- 编写两个函数,分别求由键盘输入两个整数的最大公约数和最小公倍数。用主函数调用这两个函数,并输出结果
- [置顶] 编写两个函数,分别求两个整数的最大公约数和最小公倍数
- 写两个函数,分别求两个整数的最大公约数和最小公倍数
- 输入两个整数n和m,求最大公约数和最小公倍数.Cpp
- 求两个整数的最大公约数和最小公倍数
- 习题 7.1 写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。
- (c++)写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。
- 第十一周OJ5求两个整数的最大公约数和最小公倍数
- 求两个整数的最大公约数和最小公倍数
- /*分别求出俩个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,俩个整数由键盘输入!?????怎么写,,求注释!??!*/
- 求两个整数的最大公约数和最小公倍数
- 输写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果两个整数由键盘输入。
- 利用辗转相除法求两个整数的最大公约数和最小公倍数。
- 求两个整数的最大公约数和最小公倍数
- 第11周 oj 求两个整数的最大公约数和最小公倍数
- 写两个函数,分别求两个整数的最大公约数和最小公倍数
- java基础-利用基本循环语句计算输入的两个整数的最大公约数和最小公倍数
- 编写方法,输入两个正整数m和n,求其最大公约数和最小公倍数 。
- 第十二周-求两个整数的最大公约数和最小公倍数
- 第十一周OJ(5)求两个整数的最大公约数和最小公倍数