LCM HCF | Python LCM and HCF | 1 Easy to understand

LCM and HCF(GCD) are the terminology you might have seen in your school level math. You might have heard these two things earlier and now we will try to solve them programatically.

For more python tutorials visit our playlist and also visit python programs page here.

HCF (Highest Common Factor) and GCD (Greatest Common Divisor) are the same thing. HCF is the term commonly used in British English, while GCD is the term commonly used in American English. Both terms refer to the same concept of finding the largest positive integer that divides each of the given numbers without leaving any remainder.

For example, the HCF (or GCD) of 12 and 18 is 6, because 6 is the largest number that divides both 12 and 18 without leaving any remainder.

In programming languages such as Python, the term GCD is commonly used instead of HCF. However, the two terms refer to the same concept and can be used interchangeably.

Consider the input

num1 = 12
num2 = 30

LCM (Least Common Multiple) :

print("The LCM of", num1,"and", num2,"is", calculate_lcm(num1, num2))
def calculate_lcm(x, y):
    lcm = (x*y)//calculate_hcf(x,y)
    return lcm

HCF (Highest Common Factor) :

print("The HCF of", num1,"and", num2,"is", calculate_hcf(num1, num2))
def calculate_hcf(x, y):
    while(y):
        x, y = y, x % y
    return x

For more python tutorials visit our playlist and also visit python programs page here.