Home » LCM of Two Numbers using GCD in Python

LCM of Two Numbers using GCD in Python

Learn about LCM of Two Numbers using GCD in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.


LCM of Two Numbers using GCD in Python

The HCF (highest common factor) is also known as the GCD (Greatest Common Measure). Using this formula, we can find both the GCD and the LCM at the same time. We must first determine the GCD and LCM and then apply this formula.

To find the LCM of two numbers in Python, use the program below. First, we will determine the HCF, and then the LCM will be calculated using the formula. To calculate lcm using GCD we use the following formula :

LCM(a, b) = (a*b) / HCF(a, b)

Source code:

# Python program to find the LCM using GCD

# function to find GCD 
def find_gcd(a, b):
        a, b = b, a % b
    return a

# function to find LCM
def find_lcm(a, b):
    lcm = (a*b)//find_gcd(a,b)
    return lcm

# inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# function call
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))


Enter first number: 10
Enter second number: 5
The LCM of 10 and 5 is 10

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : LCM of Two Numbers using Function in Python