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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
num1 = 12
num2 = 30
num1 = 12 num2 = 30
num1 = 12
num2 = 30

LCM (Least Common Multiple) :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print("The LCM of", num1,"and", num2,"is", calculate_lcm(num1, num2))
print("The LCM of", num1,"and", num2,"is", calculate_lcm(num1, num2))
print("The LCM of", num1,"and", num2,"is", calculate_lcm(num1, num2))
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def calculate_lcm(x, y):
lcm = (x*y)//calculate_hcf(x,y)
return lcm
def calculate_lcm(x, y): lcm = (x*y)//calculate_hcf(x,y) return lcm
def calculate_lcm(x, y):
    lcm = (x*y)//calculate_hcf(x,y)
    return lcm

HCF (Highest Common Factor) :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
print("The HCF of", num1,"and", num2,"is", calculate_hcf(num1, num2))
print("The HCF of", num1,"and", num2,"is", calculate_hcf(num1, num2))
print("The HCF of", num1,"and", num2,"is", calculate_hcf(num1, num2))
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def calculate_hcf(x, y):
while(y):
x, y = y, x % y
return x
def calculate_hcf(x, y): while(y): x, y = y, x % y return x
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.