用 C++ 查找 2 个数字的最大公因数 (HCF) 的程序
在本教程中,我们将讨论一个程序用于查找两个数字的最大公因数 (HCF)。
为此,我们将提供两个数字。我们的任务是找到这些数字的最大公因数 (HCF) 并返回它。
示例
#include <stdio.h>
//recursive call to find 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP