이 튜토리얼에서는 두 숫자의 GCD와 HCF를 찾는 프로그램에 대해 설명합니다.
이를 위해 두 개의 숫자가 제공됩니다. 우리의 임무는 주어진 두 숫자에 대한 GCD 또는 HCF(최고공약수)를 찾는 것입니다.
예시
#include <iostream>
using namespace std;
int gcd(int a, int b){
if (a == 0)
return b;
if (b == 0)
return a;
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;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
return 0;
} 출력
GCD of 98 and 56 is 14