C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)


First, let us learn how to find Highest Common Factor (HCF).

Highest Common Factor (HCF)

The greatest number divides each of the two or more numbers is called HCF or Highest Common Factor. It is also called as Greatest Common Measure(GCM) and Greatest Common Divisor(GCD).

For example,

What is the HCF of 12 and 16?

Factors of 12 = 1, 2, 3, 4, 6,12.
Factors of 16=1,2,4,8,16

Highest common factor (H.C.F) of 12 and 16 = 4.

Least Common Multiple (LCM)

For two integers x and y, denoted LCM(x,y), it is the smallest positive integer that is divisible by both x and y.

For example,

LCM(2,3) = 6 and LCM(6,10) = 30.

Example

 Live Demo

#include <stdio.h>
int main() {
   int num1, num2, x, y, temp, gcd, lcm;
   printf("Enter two integers
");    scanf("%d%d", &x, &y);    num1 = x;    num2 = y;    while (num2 != 0) {       temp = num2;       num2 = num1 % num2;       num1 = temp;    }    gcd = num1;    lcm = (x*y)/gcd;    printf("GCD of %d and %d = %d
", x, y, gcd);    printf("LCM of %d and %d = %d
", x, y, lcm);    return 0; }

Output

Upon execution, you will receive the following output −

Run 1:
Enter two integers
6 12
GCD of 6 and 12 = 6
LCM of 6 and 12 = 12
Run 2:
Enter two integers
24 36
GCD of 24 and 36 = 12
LCM of 24 and 36 = 72

Updated on: 15-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements