Python program to find the lcm of two numbers

LCM stands for Least Common Multiple, which is the smallest number that two or more numbers can be divided by evenly (without a remainder).

For example, to find the LCM of 4 and 6. We can list out the multiples of each number:

Multiples of 4: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, …

Multiples of 6: 6, 12, 18, 24, 30, 36, 42, 48, …

From the above list, we can see that the smallest number that appears in both lists is 12. So, 12 is the LCM of 4 and 6.

The formula to find LCM is:

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

where GCD stands for Greatest Common Divisor. So, to find the LCM of 4 and 6 using the formula:

LCM(4, 6) = (4 * 6) / GCD(4, 6)
GCD(4, 6) is 2, so:
LCM(4, 6) = (4 * 6) / 2 = 12

Therefore, the LCM of 4 and 6 is 12.

Using the Greatest Common Divisor method.


# This function computes GCD 
def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

num1 = 54
num2 = 24 

print("The L.C.M. is", compute_lcm(num1, num2))

Output

Enter the first number: 45
Enter the second number: 716
The LCM of 45 and 716 is 32220

In this example, we define two functions, compute_gcd() and compute_lcm(), which compute the GCD and LCM of two numbers respectively. It takes two numbers as input from the user and prints their LCM by calling the compute_lcm() function.

Conclusion

To find the LCM of two numbers using GCD method, we need to multiply the two numbers and then divide the result by their GCD.

Leave a Comment