您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C程序实现欧几里得算法

2024/2/18 17:15:25发布23次查看
问题实现欧几里得算法来查找两个整数的最大公约数 (gcd) 和最小公倍数 (lcm),并将结果与​​给定整数一起输出。
解决方案实现欧几里得算法求两个整数的最大公约数 (gcd) 和最小公倍数 (lcm) 的解决方案如下 -
求 gcd 和 lcm 的逻辑如下 -if(firstno*secondno!=0){ gcd=gcd_rec(firstno,secondno); printf("
the gcd of %d and %d is %d
",firstno,secondno,gcd); printf("
the lcm of %d and %d is %d
",firstno,secondno,(firstno*secondno)/gcd);}
调用的函数如下 -
int gcd_rec(int x, int y){ if (y == 0) return x; return gcd_rec(y, x % y);}
程序以下是 c 程序,用于实现欧几里得算法,以求两个整数的最大公约数 (gcd) 和最小公倍数 (lcm) - 现场演示
#include<stdio.h>int gcd_rec(int,int);void main(){ int firstno,secondno,gcd; printf("enter the two no.s to find gcd and lcm:"); scanf("%d%d",&firstno,&secondno); if(firstno*secondno!=0){ gcd=gcd_rec(firstno,secondno); printf("
the gcd of %d and %d is %d
",firstno,secondno,gcd); printf("
the lcm of %d and %d is %d
",firstno,secondno,(firstno*secondno)/gcd); } else printf("one of the entered no. is zero:quitting
"); } /*function for euclid's procedure*/ int gcd_rec(int x, int y){ if (y == 0) return x; return gcd_rec(y, x % y);}
输出当执行上述程序时,会产生以下结果 -
enter the two no.s to find gcd and lcm:4 8the gcd of 4 and 8 is 4the lcm of 4 and 8 is 8
以上就是c程序实现欧几里得算法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product