Python Practice › Basic Programs › Program 37
LCM of two numbers
Write a Python program to find the LCM (lowest common multiple) of two numbers.
Input
12 and 18
Output
LCM: 36
Approach
- LCM × HCF = a × b, so LCM = a × b // HCF.
Solution 1: using the HCF
import math a, b = 12, 18lcm = a * b // math.gcd(a, b)print("LCM:", lcm)Output
LCM: 36
Line by line
Line 4
12 × 18 = 216 and the HCF is 6, so the LCM is 36.
Solution 2: math.lcm
import mathprint(math.lcm(12, 18), math.lcm(4, 6, 10))Output
36 60
Line by line
Line 2
math.lcm (Python 3.9+) accepts any number of values.
Learn the concept
New to this topic? Read the lesson, then come back.