/***********************************************
* C Program to find LCM and HCF of two numbers
************************************************/
#include<stdio.h> // include stdio.h
int main()
{
int a, b;
printf("Enter two numbers: \n");
scanf("%d %d", &a, &b);
printf("HCF = %d\n", calculate_hcf(a, b));
printf("LCM = %d\n", calculate_lcm(a, b));
return 0;
}
int calculate_hcf(int smaller, int larger)
{
// Finding HCF using Euclid's Algorithm
// https://en.wikipedia.org/wiki/Euclidean_algorithm
int rem, tmp;
if(larger < smaller)
{
tmp = larger;
larger = smaller;
smaller = tmp;
}
while(1)
{
rem = larger % smaller;
if(rem == 0)
{
return smaller;
}
larger = smaller;
smaller = rem;
}
}
int calculate_lcm(int a, int b)
{
// lcm = product of two numbers / hcf
return (a * b) / calculate_hcf(a, b);
}