C Program to Find HCF and LCM

Writing a program to find the HCF and LCM of two whole numbers is a popular tutorial/question in C programing language. It gives the idea of basic loop techniques, some mathematical operations along with the fundamental input output functions of C library.


#include <stdio.h>
int main()
{
int m, n, a, b, t, hc, lc; // variable declaration
printf(" Enter two numbers: ");
scanf("%d%d", &a, &b);

m = a;
n = b;
while (n != 0)
{
t = n;
n = m % n;
m = t;
}
hc = m; // hcf
lc = (a*b)/hc; // lcm
printf(" The highest Common Factor %d and %d = %d\n", a, b, hc);
printf(" The Least Common Multiple of %d and %d = %d\n", a, b, lc);
return 0;
}

Leave a Reply