Write a C++ program to calculate the Highest Common Factor of two given number.
Program Code:
Output:
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
Post A Comment:
0 comments: