用 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