Write a C++ program to calculate the Highest Common Factor of two given number. 

Program Code:
#include <iostream>
using namespace std;
int main()
{  
int i=1;
int a, b, min, 
int HCF = 1;
cout<<"Enter first number (a) for finding HFC : ";
cin>>a;
cout<<endl;
cout<<"Enter second number (b) for finding HFC : ";

cin>>b;
cout<<endl;
min = (a<b) ? a : b;// Find minimum between two given numbers 
while( i<=min)
{
if(a%i==0 && b%i==0) // If i is a factor of both number
{
HCF = i;
}
i++;
}
cout<< "HCF of a and b is : " ;
cout<<HCF;
return 0;
}

The program output is tested on www.jdoodle.com

Output:
Enter first number (a) for finding HFC : 14
Enter second number (b) for finding HFC : 21

HCF of a and b is : 7

Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: