Wednesday, July 6, 2011

Program to find the HCF and LCM of the given numbers using functions


#include<iostream.h>
#include<conio.h>
int fhcf(int a,int b)
{
  int l,i,hcf;
  if(a>b)
     l=a;
  else
     l=b;
  for(i=2;i<=l;i++)
  {
    if(a%i==0 && b%i==0)
    {
      hcf=i;
    }
  }
  return hcf;
}
int flcm(int a,int b)
{
  int p=0,i,lcm;
  p=a*b;
  for(i=1;i<=p;i++)
  {
    if(i%a==0 && i%b==0)
    {
      lcm=i;
      break;
    }
  }
  return lcm;
}
void main()
{
  clrscr();
  int a,b,hcf,lcm;
  cout<<"Enter two numbers:"<<endl;
  cin>>a>>b;
  hcf=fhcf(a,b);
  lcm=flcm(a,b);
  cout<<"HCF is"<<hcf<<endl;
  cout<<"LCM is"<<lcm<<endl;
  getch();
}

No comments:

Post a Comment