用 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

更新時間:09-Sep-2020

463 次瀏覽

啟動你的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.