Find LCM of Two Numbers in Python
Here you will learn the example code to find LCM of two numbers in Python programming.
What is LCM definition
LCM stands for Least Common Multiple. It is the smallest positive integer that is divisible by two or more given numbers. It is calculated by finding the prime factors of each number and then multiplying them together to get the LCM.
Example to Find LCM of Two Numbers in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #Python Program to Find LCM def lcm(x, y): if x > y: greater = x else: greater = y while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 return lcm num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("The L.C.M. is", lcm(num1, num2)) |
Output 1
Enter first number: 4
Enter second number: 7
The L.C.M. is 28
Output 2
Enter first number: 4
Enter second number: 12
The L.C.M. is 12
Check out our other Python Programming Examples