Python Program to find LCM of two numbers

In this tutorial, we will learn how to find LCM of two numbers in Python. Let’s start learning.

At first, we will learn what is LCM. Then we will go for the methods that can be used to find the LCM.

LCM(Least Common Multiple)

LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers. There are many methods to find LCM of given numbers.

Method 1: First find all prime factors of both numbers, then find the union of all factors present in both numbers. Finally, return the product of elements in the union.




Example: 15 and 20 are two numbers

15=5*3

20=5*2*2

Union=5*3*2*2  = 60

Method 2: We know that the product of given numbers is equal to the product of LCM and GCD of given Numbers.

Example: 15 and 20 are two numbers

15*20=60(LCM)*5(GCD)

         A*B=LCM(A,B)*GCD(A,B)

Python Code to Find LCM of two numbers

The following code is for finding the LCM using Python.

def gcd(a,b): 
    if a == 0: 
        return b 
    return gcd(b % a, a) 
def lcm(a,b): 
    return (a*b) / gcd(a,b) 
a =int(input("Enter first number: "))
b =int(input("Enter second number: "))
print('LCM of', a, 'and', b, 'is', int(lcm(a, b)))

Output:

Enter first number: 15

Enter second number: 20

LCM of 15 and 20 is 60

So Guy’s, I hope you really enjoy this tutorial and feel free to leave a comment if you have any doubt or suggestion.

You may also learn:

Python program to find Armstrong Numbers between an interval

Python program to sort elements in ascending order using bubble sort


Leave a Reply

Your email address will not be published. Required fields are marked *