写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数。两个整数由键盘输入。
代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Greatest_Common_Divisor(int a,int b); //函数声明
int Least_Common_Multiple(int a,int b,int m); //函数声明
int a,b,m,x;
scanf("%d,%d",&a,&b);
m=Greatest_Common_Divisor(a,b);
x=Least_Common_Multiple(a,b,m);
printf("%d和%d的最大公约数是:%d\n",a,b,m);
printf("%d和%d的最小公倍数是:%d\n",a,b,x);
return 0;
}
int Greatest_Common_Divisor(int a,int b) //求最大公约数
{
int temp,r;
if(b>a) //让两个数中较大的数作为被除数
{
temp=a;
a=b;
b=temp;
}
while((r=a%b)!=0)
{
a=b; //使除数b变为被除数a
b=r; //使余数r变为除数b
}
return b;
}
int Least_Common_Multiple(int a,int b,int m) //求最小公倍数
{
return (a*b/m);
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务