Calculate HCF and LCM using C program

hcf & lcm

Before going to the topic first we have to know what is HCF and LCM, so that we can understand our c program logic.


The highest number among a group of bigger numbers (often two) that can divide into them all.


10 and 15

10’s factors: 1, 2, 5, 10

15’s factors: 1, 3, 5, 15

The highest in all lists is 5; HCF(10,15) is 5.

Also known as HCD (highest common divisor), GCD (greatest common divisor), and GCF (greater common factor).


The Least Common Multiple (LCM) of a group of numbers is the smallest number that is a multiple of all the numbers.

for example -: Let’s say you’re looking for the least common multiple of 20 and 42. Here’s how you would factor them 20 = 2 x 2 x 5 and 42 = 2 x 3 x 7

If the number just occurs in one number, then it has one occurrence. Here is a list of the most occurrences of each prime number from the previous example 2 → 2 times 3 → 1 time 5 → 1 time 7 → 1 time

Since 2 occurs twice, you’ll have to multiply it twice. Here’s what you should do to find the LCM: 2 x 2 x 3 x 5 x 7 = 420.

I hope you understand the basic concept of calculating LCM & HCF now below I am writing program to calculate HCF and LCM.

Program to calculate HCF & LCM

#include<stdio.h> // header file
int main () { // returns integer
  int a, b, x, y, t, gcd, lcm; // declaration of variable
  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);
  a = x;
  b = y;
  while ( b!= 0) {
   t = b;
   b = a % b;
   a = t;
 gcd = a;
 lcm = (x*y)/gcd;
 printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
 printf("Least common multiple of %d and %d = %d\n, x, y, lcm");
 return 0;

Still confused feel free to ask, we are here to help you

See Also

0 0 vote
Article Rating

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x