Write a program to find the HCF and LCM of the given numbers using functions

Source Code

#include<stdio.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;
}
int main (int argc, char *argv[])
{
  clrscr();
  int a,b,hcf,lcm;
  printf("\nEnter two numbers:");
  scanf("%d %d",&a,&b);
  hcf=fhcf(a,b);
  lcm=flcm(a,b);
  printf("\nHCF of %d and %d is: %d",a,b,hcf);
  printf("\nLCM of %d and %d is: %d",a,b,lcm);
  getch();
}

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code