+ 2
Program to find HCF of two no.
really interesting
3 Answers
0
# include <iostream>
# include <string >
using namespace std;
int main()
{
int a,b,c;
cout<< "Enter two nos :" <<endl;
cout<<endl;
cout<< "Enter first no. : " ;
cin>>a;
cout<< "Enter sec. no. : " ;
cin>>b;
c=a*b;
while(a!=b)
{
if (a>b)
a=a-b;
else
b=b-a;
}
cout<< "HCF = " << a<<endl;
cout<< "LCM = " << c/a<<endl;
return 0;
}
0
no.....I think you om gupta got confused actual programming is :
#include<iostream>
#include<conio>
void main ()
{
int a,b,prod=1
cout <<"enter two no";
cin>>a>>b;
for (int i=0;i<=a&&i<=b;i++)
{while (a%i==0&&b%i==0)
{a=a/i;
b/=i;
prod*=i;
}
cout<<"LCM is :"<<a*b*prod;
cout <<"HCF is:"<<prod;
}
0
Actually the simplest way is to write a recursive function:
int HCF(int a, int b) {
return b==0 ? a : HCF(b,b%a);
}
Which should be placed above the main function