Why this is Happening?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why this is Happening??

Can you explain this : int main() { int a,b,c; cout<< "Enter two nos :"<<endl; cout<< "Enter first no. : "; cin>>a; // Entered 18 cout<< "Enter sec. no. : "; cin>>b; //Entered 13 c=a*b; //c=18*13 == 234 while(a!=b) //(18!=13) ==True { if(a>b) //(18>13) == True a=a-b; // a = 18-13 == 5 else //Skipped since (a>b) b=b-a; //Skipped } cout<< "HCF = " << a<<endl; //HCF = 5 cout<< "LCM = " << c/a<<endl; // LCM = 234/5 == 46 return 0; } But the output is HCF = 1 LCM = 234 WHY??

15th Jul 2018, 8:14 AM
Vivek
Vivek - avatar
2 Answers
+ 5
Because you are using while loop which will iterate untill a and b are equal (it will stop when a and b finally becomes equal i.e 1). Use if instead of while. Hope this helpsâ˜șâ˜ș.
15th Jul 2018, 8:39 AM
Meet Mehta
Meet Mehta - avatar
0
int main(1)
15th Jul 2018, 8:19 AM
paul martin