Python Practice › Basic Programs › Program 37

LCM of two numbers

EasyBasic Programs · 37 of 40

Write a Python program to find the LCM (lowest common multiple) of two numbers.

Advertisement
Input
12 and 18
Output
LCM: 36

Approach

  1. LCM × HCF = a × b, so LCM = a × b // HCF.

Solution 1: using the HCF

Python
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

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs