在本教程中,我们将讨论一个程序来查找两个数字的HCF(最高公因数)。
为此,我们将提供两个数字。我们的任务是找到这些数字中的最高公因子(HCF)并将其返回。
#include <stdio.h>
//递归调用以查找HCF-
int gcd(int a, int b){
if (a == 0 || b == 0)
return 0;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
int main(){
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}输出结果
GCD of 98 and 56 is 14