c++ code to find hcfof all numbers in an array code example
Example 1: how to find hcf in c++
// C++ program to find GCD of two numbers
using namespace std;
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Driver program to test above function
int main()
{
int a = 98, b = 56;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);
return 0;
}
Example 2: c++ program to find gcd of 3 numbers
int main() {
int a,b,c,hcf,st;
printf("Enter three numbers : ");
scanf("%d,%d,%d", &a,&b,&c);
st=a<b?(a<c?a:c):(b<c?b:c);
for (hcf=st;hcf>=1;hcf--) {
if (a%hcf==0 && b%hcf==0 && c%hcf==0)
break;
}
printf("%d",hcf); return 0;
}