c++写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用两个函数返回相应结果

2024-12-02 17:43:35
推荐回答(1个)
回答1:

#include "stdio.h"int gcd(int a,int b){ int r; while(r=a%b) a=b,b=r; return b;}int lcm(int a,int b){ return a/gcd(a,b)*b;}int main(int argc,char *argv[]){ int x,y; printf("Please enter 2 positive integers...\n"); if(scanf("%d%d",&x,&y)!=2 || x<1 || y<1){ printf("Input error, exit...\n"); return 0; } printf("The GCD of %d & %d is %d\n",x,y,gcd(x,y)); printf("The LCM of %d & %d is %d\n",x,y,lcm(x,y)); return 0;}