C++ Program to Find LCM and HCF of two numbers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,hcf,lcm,max,min,r;
cout<<“Enter two numbers:”;
cin>>a>>b;
if(a>b)
{
max=a;
min=b;
}
else
if(b>a)
{
max=b;
min=a;
}
if(a==b)
hcf=a;
else
{
do
{
r=max%min;
max=min;
min=r;
}while(r!=0);
hcf=max;
}
lcm=(a*b)/hcf;
cout<<“nLCM=”<<lcm<<“nHCF=”<<hcf;
getch();
}

9 thoughts on “C++ Program to Find LCM and HCF of two numbers”

1. Y@shendra Shukla

sorry dude, the hcf program returns the answer as 1… otherwise, nice program man!

2. @yashendra you don't know how to calculate hcf………………if there is no hcf between two numbers than the default hcf is 1………….thanks for visitng…………..

3. Program turns out to be too long. Check out 4 this one
#include
#include

void main()
{
clrscr();

int x,y,gcd=1;

cout<<”Enter x”;cin>>x;

cout<<”Enter y”;cin>>y;

for(int i=1;i<1000;++i)
{
if((x%i==0)&&(y%i==0))
gcd=i;
}

cout<<"nGCD :"<<gcd;
cout<<"nLCM :"<<(x*y)/gcd;

getch();
}

4. @AshwinKumar G V..I use the same code..for LCm and HCF its way less complicated and easier if you know loops rather than the if/else way…

5. i am a class 11th student . i chose pcm with computer science. can u all plz help me by providing me questions which are likely to come from flow of control. 19.9.15 is the last date bcoz on that day i have my half yearly of cs. i have the book c++ for class 11 by sumita arora. iwill check the same page on 18.9.15. plz help me out !!