C语言,编写两个函数,分别求两个正整数的最大公约数和最小公倍数,结果作为函数返回值返回

主函数如下:#include<stdio.h>
void main( )
{
int gongyueshu( int a, int b );
int gongbeishu( int a, int b );
int a, b, c, d;
printf("please input two integer numbers:");
scanf("%d%d", &a, &b);
c=gongyueshu(a, b);
d=gongbeishu(a. b);
printf("gongyueshu is %d\n gongbeishu is %d",c,d);
}
int gongyueshu( int a, int b )
{ …… }
int gongbeishu( int a, int b )
{ …… }

# include <stdio.h>
int gcd(int x,int y)//最大公约数
{
if (y==0)
return x;
else
return gcd(y,x%y);
}
int lcd (int x,int y,int z)//最小公倍数
{
return x*y/z;
}
int main()
{
int a,b,c;
scanf("%d%d",&b,&c);
int d=gcd(b,c);
printf("%d %d",d,lcd(b,c,d));
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-18
#include<stdio.h>
void main( )
{
int gongyueshu( int a, int b );
int gongbeishu( int a, int b );
int a, b, c, d;
printf("please input two integer numbers:");
scanf("%d%d", &a, &b);
c=gongyueshu(a, b);
d=gongbeishu(a, b);
printf("gongyueshu is %d\n gongbeishu is %d",c,d);
}
int gongyueshu( int a, int b )
{
int t=a<b?a:b;
while(a%t||b%t)
{
t--;
}
return t;
}
int gongbeishu( int a, int b )
{
int t=a<b?b:a;
while(t%a||t%b)
{
t++;
}
return t;
}本回答被提问者采纳
第2个回答  2012-05-07
#include<stdio.h>
void main()
{int a,b,num1,num2,temp;
scanf("%d %d",&num1,&num2);
if(num1<num2){temp=num1;num1=num2;num2=temp;}
a=num1;b=num2;
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
相似回答