Wednesday, August 17, 2011

C++ program to find lcm and hcf


If you are looking for a C++ program to find lcm and hcf of 2 numbers, you will find the program here. The following program accepts 2 numbers and finds lcm and hcf of those numbers. 


Here is the program:

#include<iostream.h>
#include<conio.h>
int lcm(int,int);
int hcf(int,int);
void main()
{
clrscr();
int a,b,l,h;
cout<<"\n Enter 2 numbers\n";
cin>>a>>b;
l=lcm(a,b);
h=hcf(a,b);
cout<<"LCM:"<<l<<"\t"<<"HCF:"<<h;
getch();
}
int lcm(int x,int y)
{
  int i,big;
   big=(x>y)?x:y;


for(i=big;i<=x*y;++i)
{
if(i%x==0&&i%y==0)
{
return i;
}
}
return 0;
}
int hcf(int x,int y)
{
  int i,small,max=1;
  small=(x<y)?x:y;


for(i=1;i<=small;++i)
{
if(x%i==0&&y%i==0)
{
max=i;
}
}
return max;
}



No comments:

Post a Comment