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



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



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)))


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:

Euclidean Algorithm to find GCD in Python

One response to “Python Program to find LCM of two numbers”

  1. maira says:

    really good one … I had never posted comments but your code and your knowledge, your providing style best I request kindly some little project related to image processing also share with us,

Leave a Reply

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