Program to find HCF of two no. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Program to find HCF of two no.

really interesting

30th Oct 2016, 4:03 AM
Ayush jain
Ayush jain - avatar
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; }
30th Oct 2016, 5:50 AM
OmGupta
OmGupta - avatar
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; }
30th Oct 2016, 10:29 AM
Ayush jain
Ayush jain - avatar
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
30th Oct 2016, 10:57 AM
Hayden Lee