how to find the hcf of two numbers in c++ code example
Example 1: how to find hcf in c++
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int a = 98, b = 56;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
return 0;
}
Example 2: gcd in c++
#include<iostream>
using namespace std;
long long gcd(long long a, long long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
long long a,b;
cin>>a>>b;
cout<<gcd(a,b);
}